0

我对 NSURLConnection 有一个非常令人费解的问题。它正在另一个线程或其他东西中返回数据,事情没有按正确的顺序工作。我应该接收json数据然后解析它,但实际上是在解析器失败后接收到的数据。这是日志。

2013-09-22 14:44:40.454 WebServiceExample[39306:a0b] 解析错误:错误域 = NSCocoaErrorDomain 代码 = 3840“操作无法完成。(可可错误 3840。)”(无值。)用户信息 = 0xa0b47e0 {NSDebugDescription=No value.} 2013-09-22 14:44:41.312 WebServiceExample[39306:a0b] 成功!接收到 112 字节的数据

我正在制作一个小框架来简化我的生活以连接到 Rails API,我称之为客观 Rails OR,但我已经花了整个周末试图找出我做错了什么。这是代码:

@interface ORObject : NSObject

@property (nonatomic, retain) NSMutableDictionary *properties;
@property (nonatomic, retain) ORWebManager *webManager;
@property (nonatomic, retain) NSString *route;
@property (nonatomic, retain) NSString *singularClassName;

- (id) initWithRoute:(NSString *) route webManager:(ORWebManager *) webManager;
- (id) initWithRoute:(NSString *) route;
- (ORObject *) find:(NSInteger) identifier;
@end

下面是find的实现:

- (ORObject *) find:(NSInteger) identifier
{

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@/%ld.%@", [    ORConfig sharedInstance].baseURL, self.route, (long)identifier, self.webManager.    parser.contentType]];

    ORObject *object = [self.webManager httpGet:url];

    if (object) {
        object.route = self.route;
        object.webManager = self.webManager;
    }

    return object;
}

这里是 ORWebManager 类的 httpGet 方法

- (ORObject *) httpGet:(NSURL *)url
{
    ORGetRequester *requester = [[ORGetRequester alloc] init];
    NSMutableData *data = [requester request:url];
    return [self.parser toObject:data];
}

ORGetRequester 的超类是 ORHTTPRequester,它实现了 NSURLConnectionDelegate 协议方法

@interface ORHTTPRequester : NSObject<NSURLConnectionDelegate>
@property (nonatomic, retain) NSMutableData *receivedData;
@property (nonatomic, retain) NSMutableURLRequest *req;
@property (nonatomic, retain) NSURLConnection *connection;
@end

@implementation ORHTTPRequester
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse
    [self.receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Succeeded! Received %d bytes of data",[self.receivedData length]);
}   
@end

最后是 ORGetRequester,这就是“魔法”发生的地方。

@interface ORGetRequester : ORHTTPRequester
- (NSMutableData *) request:(NSURL *)url;
@end

@implementation ORGetRequester
- (NSMutableData *) request:(NSURL *)url
{
    self.req = [NSMutableURLRequest requestWithURL:url
                                              cachePolicy:NSURLRequestUseProtocolCachePol    icy
                                          timeoutInterval:60.0];
    self.receivedData = [[NSMutableData alloc] init];
    self.connection = [[NSURLConnection alloc] initWithRequest:self.req delegate:self];

    if (!self.connection) {
        NSLog(@"Connection Failed");
    }

    return self.receivedData;
}
@end
4

1 回答 1

1
self.connection = [[NSURLConnection alloc] initWithRequest:self.req delegate:self];

立即返回,因为它启动了异步操作。您需要在connectionDidFinishLoading:调用时解析数据。

或考虑使用:

+ (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler

当 http 操作完成时,将调用完成块。处理完成块中的数据并通知任何需要数据的对象。如果您不需要其他委托方法,这可能是一个不错的选择。

于 2013-09-22T20:19:12.800 回答