• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - RestKit RKRequests 没有立即发送

[复制链接]
菜鸟教程小白 发表于 2022-12-13 10:51:38 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在为 iPhone 应用程序使用出色的 RestKit 框架。 我有一个方法,可以将请求发送到 Web 服务。有时每 30 秒有四个或更多请求。

我的 sendMethod 看起来像:

 - (void) sendLocation {

NSString *username = [userDefaults objectForKey:kUsernameKey];    
NSString *password = [userDefaults objectForKey:kPasswordKey];
NSString *instance = [userDefaults objectForKey:kInstanceKey];
NSString *locationname = self.location.locationname;

NSString *url = [[NSString alloc] initWithFormat"http://www.someadress.com/%@", instance];

RKClient *client = [RKClient clientWithBaseURL:url username:username password:password];

// Building my JsonObject
NSDictionary *locationDictionary = [NSDictionary dictionaryWithObjectsAndKeys: username, @"username", locationname, @"locationname", nil];
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:locationDictionary, @"location", nil];

NSString *JSON = [jsonDictionary JSONRepresentation];
RKParams *params = [RKRequestSerialization serializationWithData:[JSON dataUsingEncoding:NSUTF8StringEncoding]MIMEType:RKMIMETypeJSON]; 

[client  post"/locations" params:params delegate:self];
 }

有时(尤其是在一个接一个地发送更多请求时)RKRequestQueue 对象的 count 属性值> 1。 当我的应用程序进入后台然后进入前台时,队列中的请求(进入前台时)被发送到我的 Web 服务和委托(delegate)

- (void)requestRKRequest*)request didLoadResponseRKResponse*)response {)

所有请求都会被调用。

所以问题是: 为什么 RestKit 不立即发送一些请求(当请求存储在队列中时,我的 Webservice 没有收到任何内容)???

有人知道解决方案或遇到过/遇到过同样的问题吗?



Best Answer-推荐答案


我注意到您在其中创建 RKClient 的这一行:

RKClient *client = [RKClient clientWithBaseURL:url username:username password:password];

这基本上会在每次调用 sendLocation 方法时创建新实例 - 你确定这是你想要的行为吗?如果 url、用户名和密码没有改变,您可以通过调用 [RKClient sharedClient] 访问之前创建的客户端。在您当前的方法中,会为每个新客户端创建新的请求队列。

现在回到正题。看看 RKRequestQueue 的这个属性:

/**
* The number of concurrent requests supported by this queue
* Defaults to 5
*/
@property (nonatomic) NSUInteger concurrentRequestsLimit;

正如您所看到的,默认值为 5,因此如果您的任何队列中有超过 5 个,他们将等待处理正在进行的请求。您还提到,当应用程序移动到后台时,请求会堆积起来,然后当它进入前台时,所有请求都会被调度。您可以像这样控制请求的行为方式:

- (void)backgroundUpload {
    RKRequest* request = [[RKClient sharedClient] post"somewhere" delegate:self];
    request.backgroundPolicy = RKRequestBackgroundPolicyNone; // Take no action with regard to backgrounding
    request.backgroundPolicy = RKRequestBackgroundPolicyCancel; // If the app switches to the background, cancel the request
    request.backgroundPolicy = RKRequestBackgroundPolicyContinue; // Continue the request in the background
    request.backgroundPolicy = RKRequestBackgroundPolicyRequeue; // Cancel the request and place it back on the queue for next activation
}

我找到了这个解决方案 here .向下滚动到后台上传/下载部分。

关于ios - RestKit RKRequests 没有立即发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8939378/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap