0

そこで、多くの調査の結果NSDictionary、iOS6クライアントからRailsサーバーにを送信する方法を見つけました。使用しAFNetworkingました。以下は、JSONを送信するクライアント側のコードです。ただし、操作キューが使用されている場所がわからないため、これが非同期呼び出しであるかどうかはわかりません。そうでない場合は、どうすれば非同期呼び出しにできますか?

  -(void)sendEntryToServerAsJSON:(EntryParent*)_entryToBeSaved
  {
     NSDictionary* dictToBeSerialized = [_entryToBeSaved convertEntryParentObjToDict];


     [[appAPIClient sharedClient]postPath:@"entries.json"
                          parameters:dictToBeSerialized
                             success:^(AFHTTPRequestOperation *operation, id responseObject) {
       NSLog(@"Successfully sent JSON %@", [responseObject description]);
      }
                             failure:^(AFHTTPRequestOperation *operation, NSError *error)    {
       NSLog(@"Could not send JSON %@", [error description]);
      }];
  }

これがAFHTTPClientの私の実装です

 + (appAPIClient *)sharedClient
 {
   NSLog(@"Inside appAPIClient sharedclient ");
   static appAPIClient *_sharedClient = nil;
   static dispatch_once_t oncePredicate;
   dispatch_once(&oncePredicate, ^{
    _sharedClient = [[self alloc]
                     initWithBaseURL:[NSURL URLWithString:URL_STR]];
  });

   return _sharedClient;
 }
 //==============================================================================

 - (id)initWithBaseURL:(NSURL *)url
 {
   self = [super initWithBaseURL:url];
   if (!self)
  {
    return nil;
   }
   NSLog(@"init with base url - appAPIClient");
   [self registerHTTPOperationClass:[AFJSONRequestOperation class]];

   // Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
[self setDefaultHeader:@"Accept" value:@"application/json"];

   return self;
}

このコードを改善し、非同期にするためのフィードバックをいただければ幸いです。

4

1 に答える 1

0

に)

リクエストはメインスレッドのrunloopに追加されるため、スレッドやキューがなくても非同期になります。runloopsはCのselectメソッドのようなものです

参照:
https ://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSRunLoop_Class/Reference/Reference.html

b)別の質問にしてください:)

于 2013-01-27T19:16:16.560 に答える