菜鸟教程小白 发表于 2022-12-13 10:51:38

ios - RestKit RKRequests 没有立即发送


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

<p>我的 sendMethod 看起来像:</p>

<pre><code> - (void) sendLocation {

NSString *username = ;   
NSString *password = ;
NSString *instance = ;
NSString *locationname = self.location.locationname;

NSString *url = [ initWithFormat:@&#34;http://www.someadress.com/%@&#34;, instance];

RKClient *client = ;

// Building my JsonObject
NSDictionary *locationDictionary = ;
NSDictionary *jsonDictionary = ;

NSString *JSON = ;
RKParams *params = MIMEType:RKMIMETypeJSON];

;
}
</code></pre>

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

<pre><code>- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {)
</code></pre>

<p>所有请求都会被调用。</p>

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

<p>有人知道解决方案或遇到过/遇到过同样的问题吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我注意到您在其中创建 RKClient 的这一行:</p>

<pre><code>RKClient *client = ;
</code></pre>

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

<p>现在回到正题。看看 <em>RKRequestQueue</em> 的这个属性:</p>

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

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

<pre><code>- (void)backgroundUpload {
    RKRequest* request = [ post:@&#34;somewhere&#34; 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
}
</code></pre>

<p>我找到了这个解决方案 <a href="http://mobile.tutsplus.com/tutorials/iphone/advanced-restkit-development_iphone-sdk/" rel="noreferrer noopener nofollow">here</a> .向下滚动到<em>后台上传/下载</em>部分。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - RestKit RKRequests 没有立即发送,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/8939378/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/8939378/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - RestKit RKRequests 没有立即发送