NSURLConnection で非常に不可解な問題が発生しています。別のスレッドまたは何かでデータを返しています。物事は適切な順序で機能していません。json データを受信してから解析することになっていますが、実際にはパーサーが失敗した後にデータが受信されています。これがログです。
2013-09-22 14:44:40.454 WebServiceExample[39306:a0b] 解析エラー: エラー Domain=NSCocoaErrorDomain Code=3840 「操作を完了できませんでした。(Cocoa エラー 3840)」(値なし) UserInfo=0xa0b47e0 {NSDebugDescription=値なし} 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