0

因此,经过大量研究,我想出了如何NSDictionary从我的 iOS 6 客户端发送一个到我的 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

一)

请求被添加到主线程运行循环中,因此它们是异步的,没有线程或队列。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 回答