iphone - 让 Restkit 教程代码工作

标签 iphone ios json restkit

我对 iOS 开发相对较新,并试图熟悉 RestKit,所以如果我遗漏了一些明显的东西,请多多包涵。

我们正在开发一个 iOS 5 应用程序,它将接收 JSON 并将其映射到对象(启用 ARC,如果重要的话)。在寻找 Restkit 的教程时,我遇到了 this one .但是,那里的示例代码的简化版本(“使用 RKClient 的真实示例”部分中的那个)失败并出现以下错误:

2012-01-14 22:15:55.124 Test_StaticLibInApp[789:707] *** Assertion failure in -[RKRequestQueue removeRequest:decrementCounter:], /Users/m/Documents/devel/ios/RestKit/Code/Network/RKRequestQueue.m:350
2012-01-14 22:15:55.129 Test_StaticLibInApp[789:707] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempted to decrement loading count below zero'

确切地说,这是我尝试过的代码:
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    RKClient *client = [RKClient clientWithBaseURL:@"some_url"];
    RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);

    NSDictionary *queryParameters = [NSDictionary dictionaryWithObjectsAndKeys:nil];
    NSString *getResourcePath = RKPathAppendQueryParams(@"some_url_segment", queryParameters);
    RKRequest *request = [client get:getResourcePath delegate:self];
    [request sendAsynchronously];
}

- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response
{
    id jsonParser = [[RKParserRegistry sharedRegistry] parserForMIMEType:RKMIMETypeJSON];
    NSError *error = nil;
    id parsedResponse = [jsonParser objectFromString:[response bodyAsString] error:&error];
    if (error == nil)
    {
        NSLog(@"GET returned with HTTP Code %d and parsedContent: %@", [response statusCode], parsedResponse);
    }
    else
    {
        NSLog(@"Error: %@", error);
    }
}

- (void)request:(RKRequest *)request didFailLoadWithError:(NSError *)error
{
    NSLog(@"Failure of GET with error %@.", error);
}

这是跟踪输出:
2012-01-14 22:15:54.746 Test_StaticLibInApp[789:707] I restkit:RKLog.m:30 RestKit initialized...
2012-01-14 22:15:54.765 Test_StaticLibInApp[789:707] D restkit.network:RKRequest.m:365 Sending asynchronous GET request to URL <url>.
2012-01-14 22:15:54.771 Test_StaticLibInApp[789:707] T restkit.network:RKRequest.m:313 Prepared GET URLRequest '<NSMutableURLRequest url>'. HTTP Headers: {
    "Content-Length" = 0;
}. HTTP Body: .
2012-01-14 22:15:54.852 Test_StaticLibInApp[789:707] I restkit.network.reachability:RKReachabilityObserver.m:369 Network availability has been determined for reachability observer <RKReachabilityObserver: 0x15c920 host=<server ip> isReachabilityDetermined=YES isMonitoringLocalWiFi=448340 reachabilityFlags=-R ------d>
2012-01-14 22:15:54.857 Test_StaticLibInApp[789:707] D restkit.network:RKClient.m:388 Reachability to host '<server ip>' determined for client <RKClient: 0x158740>, unsuspending queue <RKRequestQueue: 0x15b260 name=(null) suspended=YES requestCount=1 loadingCount=0/5>
2012-01-14 22:15:55.037 Test_StaticLibInApp[789:707] D restkit.network:RKResponse.m:189 NSHTTPURLResponse Status Code: 200
2012-01-14 22:15:55.044 Test_StaticLibInApp[789:707] D restkit.network:RKResponse.m:190 Headers: {
    "Content-Type" = "application/json";
    Date = "Sat, 14 Jan 2012 20:16:21 GMT";
    Server = "Apache-Coyote/1.1";
    "Transfer-Encoding" = Identity;
}
2012-01-14 22:15:55.048 Test_StaticLibInApp[789:707] T restkit.network:RKResponse.m:195 Read response body: <json text here>
2012-01-14 22:15:55.059 Test_StaticLibInApp[789:707] I restkit.network:RKRequest.m:565 Status Code: 200
2012-01-14 22:15:55.066 Test_StaticLibInApp[789:707] D restkit.network:RKRequest.m:566 Body: <json text here>
2012-01-14 22:15:55.106 Test_StaticLibInApp[789:707] GET returned with HTTP Code 200 and parsedContent: <json text here>
2012-01-14 22:15:55.124 Test_StaticLibInApp[789:707] *** Assertion failure in -[RKRequestQueue removeRequest:decrementCounter:], /Users/m/Documents/devel/ios/RestKit/Code/Network/RKRequestQueue.m:350
2012-01-14 22:15:55.129 Test_StaticLibInApp[789:707] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempted to decrement loading count below zero'
*** First throw call stack:
(0x33fac8bf 0x341fc1e5 0x33fac7b9 0x3595b3b3 0x1da7f 0x1e7a3 0x3598150f 0x33f78577 0x33f040cf 0x358f53fb 0x1818b 0x2064b 0x359b8c39 0x359106e9 0x359106b3 0x359105d5 0x31fbe8a5 0x31fb3545 0x31fb3243 0x31fb3179 0x33f80b03 0x33f802cf 0x33f7f075 0x33f024dd 0x33f023a5 0x30786fcd 0x37383743 0x2c81 0x2c18)

这个错误是否与使用 ARC 编译的应用程序有关?对于 Restkit 安装,我从 git master 分支获取文件并按照安装说明进行操作,没有更改其任何设置。

提前感谢您的帮助。

最佳答案

线

[请求发送异步]

不需要。

[客户端获取:getResourcePath 委托(delegate):自己]

已经发送请求,并减少请求计数。你得到

原因:'试图将加载计数减少到零以下'

因为这。

关于iphone - 让 Restkit 教程代码工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8865471/

相关文章:

ios - 可以将 Storyboard中的导航右栏按钮与编码的按钮结合起来吗?

ios - 我们如何在 iOS 应用程序中添加视频而不是启动图像?

json - Rust 变量 'does not live long enough' 和 'conflicting requirements'

ios - 如何在 iOS 上获取音量级别和音量更改通知?

ios - UITabbarController firSTLy 选定的选项卡表现异常

iphone - 对于 iPhone OS 4.0 "dateFromString"NSDateFormatter 方法返回 nil

ios - 避免在点击 MKAnnotation 时显示标注

ios - 无论如何,异步 NSURLconnection 是否有助于实时更新或向 iOS 应用程序中的服务器发送持续请求?

jquery - jsTree 未使用 "inline"json 数据进行渲染

java - 将 json 写入 String 而不是 java 中的文件