2012-01-20 5 views
2

私はiPhoneアプリケーションに素晴らしいRestKitフレームワークを使用しています。 私はWebサービスにリクエストを送信するメソッドを持っています。場合によっては30秒ごとに4つ以上の要求が発生することがあります。RestKit RKRequestsはすぐに送信されません

マイsendMethodは、次のようになります。私のアプリケーションがバックグラウンドに入り、その後、フォアグラウンドに入るとRKRequestQueueオブジェクトのCountプロパティの値が> 1 である(別の後に多くのリクエストを送信する場合は特に)時々

- (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]; 
} 

キュー(フォアグラウンドが入力されている)での要求は私のWebサービスに送信され、すべての要求のためのデリゲート

- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {) 

が呼び出されます。

質問: なぜRestKitはいくつかのリクエストをすぐに送信しません(リクエストがキューに格納されている間、Webサービスが何も受信しません)???

解決策を知っている人もいますか、同じ問題がありましたか?

答えて

3

私はあなたがRKClientを作成するには、この行に気づいた:これは、基本的に新しいインスタンスを毎回作成

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

sendLocationメソッドが呼び出された - これはあなたの希望の行動であるあなたは確かにありますか? URL、ユーザー名、パスワードが変更されない場合は、[RKClient sharedClient]を呼び出して、以前作成したクライアントにアクセスできます。現在の方法では、新しいクライアントごとに新しい要求キューが作成されます。

今すぐポイントに戻ってください。あなたは5に、このデフォルト値を見ることができるように

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

ので、あなたがあなたのキューのいずれかにそれ以上のものを持っている場合は、進行中の要求が処理されるまで、彼らは待機します:RKRequestQueueのこの性質に見てみましょう。また、アプリケーションがバックグラウンドに移動したときに要求がスタックアップし、フォアグラウンドに入るとすべてがディスパッチされることも説明しました。あなたはあなたの要求がこのように振る舞う方法を制御することができます。

- (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を発見しました。 の背景のアップロード/ダウンロードセクションまでスクロールします。