ios5 - RestKit loadObjectsAtResourcePath :delegate works once, 但以后再也不会

标签 ios5 xcode4 restkit

我对 Objective-C 和 XCode 还很陌生,但对 RestKit 还是比较陌生,尽管我已经用 Java 编程了 12 年。

我有一个连接到 RestKit 的 IOS 应用程序,并且从休息服务器加载的操作仅在第一次工作,如果我返回导航 Controller 并再次尝试相同的操作,loadObjectsAtResourcePath:delegate方法被调用,但没有一个委托(delegate)方法被调用(我实现了所有这些方法只是为了在它们发生时记录)。

我觉得我错过了 Restkit 的一些基本内容,也许是它的异步性质,因为映射显然第一次工作正常,所以后续调用一定会丢失在异步队列中。

我用谷歌搜索了这个,看到类似的人被类似的东西困住了,但都试过了,似乎没有什么不同。缓存被禁用,objectLoaderDidLoadUnexpectedResponsedidFailWithError永远不会被调用,它会掉到地板上。

代码来源:

我的 XCode 项目在 github here

执行加载调用的特定 View Controller 是 here (见 setUpRestKit 方法)

调用顺序如下:

  • 用户填写主机名和端口,点击连接
  • ESRKConnectionViewController.m通过 接听电话prepareForSegue 并用这个地址设置共享的 RKClient(虽然我后来发现 RKClient 用得不多,但 RKOBjectManager 似乎只需要一个 baseURL,所以那里有点多余)
  • ESRKServerViewController.m 然后在 RKObjectManager 上设置映射并调用 loadObjectsAtResourcePath 将自己作为委托(delegate)传递(是的,我意识到这些映射可能应该在 App Delegate 的开始时配置,但我看不出再次设置它不仅仅是冗余代码)
  • objectLoader:didLoadObjects 然后回调负责获取映射对象并为表格 View 配置模型 - 这只会触发一次。

  • 启用日志,这是我看到的模式,在第一个调用完成时以及第二个调用何时开始等上带有注释(为了清楚起见,此处修剪了时间戳):
    ElasticSearchHeadIOS[25064:fb03] I restkit:RKLog.m:32 RestKit initialized...
    ElasticSearchHeadIOS[25064:fb03] I restkit.support:RKCache.m:178 Invalidating cache at path: /Users/paulsmith/Library/Application Support/iPhone Simulator/5.1/Applications/BDB8B59F-8307-4F18-A4E7-C55849618D77/Library/Caches/RKClientRequestCache-localhost/SessionStore
    ElasticSearchHeadIOS[25064:fb03] I restkit.network.reachability:RKReachabilityObserver.m:155 Reachability observer initialized with hostname localhost
    ElasticSearchHeadIOS[25064:fb03] D restkit.network:RKClient.m:271 Reachability observer changed for client <RKClient: 0x80756e0>, suspending queue <RKRequestQueue: 0x8082600 name=(null) suspended=YES requestCount=0 loadingCount=0/5> until reachability to host 'localhost' can be determined
    ElasticSearchHeadIOS[25064:fb03] Connecting to http://localhost:9200/ -- http://localhost:9200/ -- http://localhost:9200/
    ElasticSearchHeadIOS[25064:fb03] I restkit.support:RKCache.m:178 Invalidating cache at path: /Users/paulsmith/Library/Application Support/iPhone Simulator/5.1/Applications/BDB8B59F-8307-4F18-A4E7-C55849618D77/Library/Caches/RKClientRequestCache-localhost/SessionStore
    ElasticSearchHeadIOS[25064:fb03] I restkit.network.reachability:RKReachabilityObserver.m:155 Reachability observer initialized with hostname localhost
    ElasticSearchHeadIOS[25064:fb03] D restkit.network:RKClient.m:271 Reachability observer changed for client <RKClient: 0x8087840>, suspending queue <RKRequestQueue: 0x8087a40 name=(null) suspended=YES requestCount=0 loadingCount=0/5> until reachability to host 'localhost' can be determined
    ElasticSearchHeadIOS[25064:fb03] I restkit.network.reachability:RKReachabilityObserver.m:369 Network availability has been determined for reachability observer <RKReachabilityObserver: 0x8088980 host=localhost isReachabilityDetermined=YES isMonitoringLocalWiFi=1039984 reachabilityFlags=-R -----ld>
    ElasticSearchHeadIOS[25064:fb03] D restkit.network:RKClient.m:401 Reachability to host 'localhost' determined for client <RKClient: 0x8087840>, unsuspending queue <RKRequestQueue: 0x8087a40 name=(null) suspended=YES requestCount=1 loadingCount=0/5>
    ElasticSearchHeadIOS[25064:fb03] D restkit.network:RKRequest.m:435 Sending asynchronous GET request to URL http://localhost:9200/_cluster/state.
    ElasticSearchHeadIOS[25064:fb03] T restkit.network:RKRequest.m:381 Prepared GET URLRequest '<NSMutableURLRequest http://localhost:9200/_cluster/state>'. HTTP Headers: {
        Accept = "application/json";
        "Content-Length" = 0;
    }. HTTP Body: .
    ElasticSearchHeadIOS[25064:fb03] I restkit.network.reachability:RKReachabilityObserver.m:369 Network availability has been determined for reachability observer <RKReachabilityObserver: 0x6c5c530 host=localhost isReachabilityDetermined=YES isMonitoringLocalWiFi=1039984 reachabilityFlags=-R -----ld>
    ElasticSearchHeadIOS[25064:fb03] D restkit.network:RKClient.m:401 Reachability to host 'localhost' determined for client <RKClient: 0x80756e0>, unsuspending queue <RKRequestQueue: 0x8082600 name=(null) suspended=YES requestCount=0 loadingCount=0/5>
    ElasticSearchHeadIOS[25064:fb03] D restkit.network:RKResponse.m:195 NSHTTPURLResponse Status Code: 200
    ElasticSearchHeadIOS[25064:fb03] D restkit.network:RKResponse.m:196 Headers: {
        "Content-Length" = 4462;
        "Content-Type" = "application/json; charset=UTF-8";
    }
     ElasticSearchHeadIOS[25064:fb03] T restkit.network:RKResponse.m:203 Read response body: {"cluster_name":"paul","master_node":"FGewvE3-TEKjXugOGJpWmQ","blocks":{},"nodes":{"FGewvE3-TEKjXugOGJpWmQ":{"name":"Namor the Sub-Mariner","transport_address":"inet[/192.168.0.12:9300]","attributes":{}}},"metadata":{"templates":{},"indices":{"melpaul":{"state":"open","settings":{"index.number_of_replicas":"1","index.number_of_shards":"5","index.version.created":"190299"},"mappings":{"project":{"properties":{"projectAddress":{"type":"string"},"id":{"type":"string"},"suburb":{"type":"string"},"name":{"type":"string"},"state":{"type":"string"},"postalAddress":{"type":"string"},"ownerOrganizationID":{"type":"long"},"shortName":{"type":"string"},"deliveryAddress":{"type":"string"},"country":{"type":"string"}}},"organization":{"properties":{"id":{"type":"string"},"tradingName":{"type":"string"},"name":{"type":"string"},"extParticipant":{"type":"string"},"code":{"type":"string"}}},"task":{"properties":{"aggregateCount":{"type":"long"},"dueDateIso":{"type":"string"},"assignedByUser":{"type":"string"},"pertinent":{"type":"boolean"},"assignedByUserId":{"type":"long"},"assignedBy":{"type":"string"},"projectId":{"type":"long"},"type":{"type":"string"},"id":{"type":"string"},"creationDate":{"type":"long"},"assignedToUserId":{"type":"long"},"description":{"type":"string"},"featureId":{"type":"string"},"aggregated":{"type":"boolean"},"assignedToUser":{"type":"string"},"projectName":{"type":"string"},"dueDate":{"type":"long"},"referenceLabel":{"type":"string"}}},"user":{"properties":{"sortlastName":{"type":"string"},"position":{"type":"string"},"lastName":{"type":"string"},"phone":{"type":"string"},"accessEnabled":{"type":"string"},"orgName":{"type":"string"},"directoryVisibility":{"type":"string"},"id":{"type":"string"},"orgId":{"type":"string"},"orgTradingName":{"type":"string"},"title":{"type":"string"},"divisionName":{"type":"string"},"email":{"type":"string"},"sortfirstName":{"type":"string"},"userName":{"type":"string"},"extParticipant":{"type":"string"},"firstName":{"type":"string"},"accountUnlocked":{"type":"string"},"mobile":{"type":"string"}}}},"aliases":[]}}},"routing_table":{"indices":{"melpaul":{"shards":{"0":[{"state":"STARTED","primary":true,"node":"FGewvE3-TEKjXugOGJpWmQ","relocating_node":null,"shard":0,"index":"melpaul"},{"state":"UNASSIGNED","primary":false,"node":null,"relocating_node":null,"shard":0,"index":"melpaul"}],"1":[{"state":"STARTED","primary":true,"node":"FGewvE3-TEKjXugOGJpWmQ","relocating_node":null,"shard":1,"index":"melpaul"},{"state":"UNASSIGNED","primary":false,"node":null,"relocating_node":null,"shard":1,"index":"melpaul"}],"2":[{"state":"STARTED","primary":true,"node":"FGewvE3-TEKjXugOGJpWmQ","relocating_node":null,"shard":2,"index":"melpaul"},{"state":"UNASSIGNED","primary":false,"node":null,"relocating_node":null,"shard":2,"index":"melpaul"}],"3":[{"state":"STARTED","primary":true,"node":"FGewvE3-TEKjXugOGJpWmQ","relocating_node":null,"shard":3,"index":"melpaul"},{"state":"UNASSIGNED","primary":false,"node":null,"relocating_node":null,"shard":3,"index":"melpaul"}],"4":[{"state":"STARTED","primary":true,"node":"FGewvE3-TEKjXugOGJpWmQ","relocating_node":null,"shard":4,"index":"melpaul"},{"state":"UNASSIGNED","primary":false,"node":null,"relocating_node":null,"shard":4,"index":"melpaul"}]}}}},"routing_nodes":{"unassigned":[{"state":"UNASSIGNED","primary":false,"node":null,"relocating_node":null,"shard":0,"index":"melpaul"},{"state":"UNASSIGNED","primary":false,"node":null,"relocating_node":null,"shard":1,"index":"melpaul"},{"state":"UNASSIGNED","primary":false,"node":null,"relocating_node":null,"shard":2,"index":"melpaul"},{"state":"UNASSIGNED","primary":false,"node":null,"relocating_node":null,"shard":3,"index":"melpaul"},{"state":"UNASSIGNED","primary":false,"node":null,"relocating_node":null,"shard":4,"index":"melpaul"}],"nodes":{"FGewvE3-TEKjXugOGJpWmQ":[{"state":"STARTED","primary":true,"node":"FGewvE3-TEKjXugOGJpWmQ","relocating_node":null,"shard":0,"index":"melpaul"},{"state":"STARTED","primary":true,"node":"FGewvE3-TEKjXugOGJpWmQ","relocating_node":null,"shard":1,"index":"melpaul"},{"state":"STARTED","primary":true,"node":"FGewvE3-TEKjXugOGJpWmQ","relocating_node":null,"shard":2,"index":"melpaul"},{"state":"STARTED","primary":true,"node":"FGewvE3-TEKjXugOGJpWmQ","relocating_node":null,"shard":3,"index":"melpaul"},{"state":"STARTED","primary":true,"node":"FGewvE3-TEKjXugOGJpWmQ","relocating_node":null,"shard":4,"index":"melpaul"}]}},"allocations":[]}
     ElasticSearchHeadIOS[25064:1400b] D restkit.network:RKObjectLoader.m:262 Beginning object mapping activities within GCD queue labeled: org.restkit.ObjectMapping
     ElasticSearchHeadIOS[25064:1400b] D restkit.network:RKObjectLoader.m:250 No object mapping provider, using mapping provider from parent object manager to perform KVC mapping
     ElasticSearchHeadIOS[25064:1400b] T restkit.network:RKObjectLoader.m:187 bodyAsString: {"cluster_name":"paul","master_node":"FGewvE3-TEKjXugOGJpWmQ","blocks":{},"nodes":{"FGewvE3-TEKjXugOGJpWmQ":{"name":"Namor the Sub-Mariner","transport_address":"inet[/192.168.0.12:9300]","attributes":{}}},"metadata":{"templates":{},"indices":{"melpaul":{"state":"open","settings":{"index.number_of_replicas":"1","index.number_of_shards":"5","index.version.created":"190299"},"mappings":{"project":{"properties":{"projectAddress":{"type":"string"},"id":{"type":"string"},"suburb":{"type":"string"},"name":{"type":"string"},"state":{"type":"string"},"postalAddress":{"type":"string"},"ownerOrganizationID":{"type":"long"},"shortName":{"type":"string"},"deliveryAddress":{"type":"string"},"country":{"type":"string"}}},"organization":{"properties":{"id":{"type":"string"},"tradingName":{"type":"string"},"name":{"type":"string"},"extParticipant":{"type":"string"},"code":{"type":"string"}}},"task":{"properties":{"aggregateCount":{"type":"long"},"dueDateIso":{"type":"string"},"assignedByUser":{"type":"string"},"pertinent":{"type":"boolean"},"assignedByUserId":{"type":"long"},"assignedBy":{"type":"string"},"projectId":{"type":"long"},"type":{"type":"string"},"id":{"type":"string"},"creationDate":{"type":"long"},"assignedToUserId":{"type":"long"},"description":{"type":"string"},"featureId":{"type":"string"},"aggregated":{"type":"boolean"},"assignedToUser":{"type":"string"},"projectName":{"type":"string"},"dueDate":{"type":"long"},"referenceLabel":{"type":"string"}}},"user":{"properties":{"sortlastName":{"type":"string"},"position":{"type":"string"},"lastName":{"type":"string"},"phone":{"type":"string"},"accessEnabled":{"type":"string"},"orgName":{"type":"string"},"directoryVisibility":{"type":"string"},"id":{"type":"string"},"orgId":{"type":"string"},"orgTradingName":{"type":"string"},"title":{"type":"string"},"divisionName":{"type":"string"},"email":{"type":"string"},"sortfirstName":{"type":"string"},"userName":{"type":"string"},"extParticipant":{"type":"string"},"firstName":{"type":"string"},"accountUnlocked":{"type":"string"},"mobile":{"type":"string"}}}},"aliases":[]}}},"routing_table":{"indices":{"melpaul":{"shards":{"0":[{"state":"STARTED","primary":true,"node":"FGewvE3-TEKjXugOGJpWmQ","relocating_node":null,"shard":0,"index":"melpaul"},{"state":"UNASSIGNED","primary":false,"node":null,"relocating_node":null,"shard":0,"index":"melpaul"}],"1":[{"state":"STARTED","primary":true,"node":"FGewvE3-TEKjXugOGJpWmQ","relocating_node":null,"shard":1,"index":"melpaul"},{"state":"UNASSIGNED","primary":false,"node":null,"relocating_node":null,"shard":1,"index":"melpaul"}],"2":[{"state":"STARTED","primary":true,"node":"FGewvE3-TEKjXugOGJpWmQ","relocating_node":null,"shard":2,"index":"melpaul"},{"state":"UNASSIGNED","primary":false,"node":null,"relocating_node":null,"shard":2,"index":"melpaul"}],"3":[{"state":"STARTED","primary":true,"node":"FGewvE3-TEKjXugOGJpWmQ","relocating_node":null,"shard":3,"index":"melpaul"},{"state":"UNASSIGNED","primary":false,"node":null,"relocating_node":null,"shard":3,"index":"melpaul"}],"4":[{"state":"STARTED","primary":true,"node":"FGewvE3-TEKjXugOGJpWmQ","relocating_node":null,"shard":4,"index":"melpaul"},{"state":"UNASSIGNED","primary":false,"node":null,"relocating_node":null,"shard":4,"index":"melpaul"}]}}}},"routing_nodes":{"unassigned":[{"state":"UNASSIGNED","primary":false,"node":null,"relocating_node":null,"shard":0,"index":"melpaul"},{"state":"UNASSIGNED","primary":false,"node":null,"relocating_node":null,"shard":1,"index":"melpaul"},{"state":"UNASSIGNED","primary":false,"node":null,"relocating_node":null,"shard":2,"index":"melpaul"},{"state":"UNASSIGNED","primary":false,"node":null,"relocating_node":null,"shard":3,"index":"melpaul"},{"state":"UNASSIGNED","primary":false,"node":null,"relocating_node":null,"shard":4,"index":"melpaul"}],"nodes":{"FGewvE3-TEKjXugOGJpWmQ":[{"state":"STARTED","primary":true,"node":"FGewvE3-TEKjXugOGJpWmQ","relocating_node":null,"shard":0,"index":"melpaul"},{"state":"STARTED","primary":true,"node":"FGewvE3-TEKjXugOGJpWmQ","relocating_node":null,"shard":1,"index":"melpaul"},{"state":"STARTED","primary":true,"node":"FGewvE3-TEKjXugOGJpWmQ","relocating_node":null,"shard":2,"index":"melpaul"},{"state":"STARTED","primary":true,"node":"FGewvE3-TEKjXugOGJpWmQ","relocating_node":null,"shard":3,"index":"melpaul"},{"state":"STARTED","primary":true,"node":"FGewvE3-TEKjXugOGJpWmQ","relocating_node":null,"shard":4,"index":"melpaul"}]}},"allocations":[]}
     ElasticSearchHeadIOS[25064:1400b]  willMapData
     ElasticSearchHeadIOS[25064:fb03] did finish loading
    

    好的,显示从上述调用返回的映射数据的表格 View 现已完成,我可以在屏幕上正确看到结果。

    我点击导航 Controller ,再次尝试相同的请求,这就是日志的作用。此处的日志在我单击连接按钮并启动与上面相同的调用后立即开始(在上面的 block 和下面的 block 之间没有其他日志出现)。
    I restkit.support:RKCache.m:178 Invalidating cache at path: /Users/paulsmith/Library/Application Support/iPhone Simulator/5.1/Applications/BDB8B59F-8307-4F18-A4E7-C55849618D77/Library/Caches/RKClientRequestCache-localhost/SessionStore
    I restkit.network.reachability:RKReachabilityObserver.m:155 Reachability observer initialized with hostname localhost
    D restkit.network:RKClient.m:271 Reachability observer changed for client <RKClient: 0x808ce90>, suspending queue <RKRequestQueue: 0x80834c0 name=(null) suspended=YES requestCount=0 loadingCount=0/5> until reachability to host 'localhost' can be determined
    D restkit.network:RKClient.m:256 Reachability observer changed for RKClient <RKClient: 0x80756e0>, disposing of previous instance: <RKReachabilityObserver: 0x6c5c530 host=localhost isReachabilityDetermined=YES isMonitoringLocalWiFi=1039984 reachabilityFlags=-R -----ld>
    Connecting to http://localhost:9200/ -- http://localhost:9200/ -- http://localhost:9200/
    I restkit.support:RKCache.m:178 Invalidating cache at path: /Users/paulsmith/Library/Application Support/iPhone Simulator/5.1/Applications/BDB8B59F-8307-4F18-A4E7-C55849618D77/Library/Caches/RKClientRequestCache-localhost/SessionStore
    I restkit.network.reachability:RKReachabilityObserver.m:155 Reachability observer initialized with hostname localhost
    D restkit.network:RKClient.m:271 Reachability observer changed for client <RKClient: 0x81b0bc0>, suspending queue <RKRequestQueue: 0x81b0d20 name=(null) suspended=YES requestCount=0 loadingCount=0/5> until reachability to host 'localhost' can be determined
    D restkit.network:RKClient.m:256 Reachability observer changed for RKClient <RKClient: 0x81b0bc0>, disposing of previous instance: <RKReachabilityObserver: 0x81b1c80 host=localhost isReachabilityDetermined=NO isMonitoringLocalWiFi=1039984 reachabilityFlags=-- ------->
    I restkit.network.reachability:RKReachabilityObserver.m:369 Network availability has been determined for reachability observer <RKReachabilityObserver: 0x807f2f0 host=localhost isReachabilityDetermined=YES isMonitoringLocalWiFi=1039984 reachabilityFlags=-R -----ld>
    D restkit.network:RKClient.m:401 Reachability to host 'localhost' determined for client <RKClient: 0x808ce90>, unsuspending queue <RKRequestQueue: 0x80834c0 name=(null) suspended=YES requestCount=0 loadingCount=0/5>
    

    这是 Jing screencast它在模拟器中的样子(你可以看到委托(delegate)方法的断点第一次被调用,但第二次没有)

    最佳答案

    这是一个保留问题。您正在创建对象的本地副本,而不是将其分配给任何导致它被保留的东西。这可能是 RKObjectManager 之类的。创建一个属性,例如 myObjectManager 并像这样分配
    self.myObjectManager = [[RkObjectManager alloc] init];

    确保您学习并了解内存管理。使用 ARC,但您仍然需要对内存管理有基本的了解!

    高温高压

    关于ios5 - RestKit loadObjectsAtResourcePath :delegate works once, 但以后再也不会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10458490/

    相关文章:

    iphone - NSArray 与 NSDictionary 查找

    ios - SQLcipher如何创建加密数据库?

    xcode - 脚本 Xcode

    iOS 和 RestKit : How to get a text/html response right?

    ios - RestKit:重新验证后如何重新提交失败的请求?

    iphone - NSJSONSerialization 中没有返回

    objective-c - 获取iOS中两个日期之间的日期名称?

    iphone - Xcode 4 中没有 gcc 4.2 编译器选项?

    objective-c - 是否可以抑制 Xcode 4 静态分析器警告?

    objective-c - 设置对象映射的未弃用方法