我面临一个非常奇怪的问题,我希望有人能帮助我。
我正在使用带有 CoreData 设置的 Restkit (0.20),并且我拥有 responseDescriptor 的所有属性/关系映射设置。这是正确设置的,因为当我第一次调用 getObjectsAtPath:parameters 时,我得到了正确的结果并且它能够映射到正确的对象。
但是,在随后的 getObjectsAtPath 调用中,mappingResult 由于某种原因会产生空结果,即使我可以看到来自服务器的请求很好,并且相同的结果 JSON 被发送回客户端。
有没有人看到这个问题??!?!!?
这是映射代码:
RKEntityMapping *userMapping = [User getEntityMapping:objectManager.managedObjectStore];
RKEntityMapping *voteMapping = [Vote getEntityMapping:objectManager.managedObjectStore];
RKEntityMapping *commentMapping = [Comment getEntityMapping:objectManager.managedObjectStore];
RKEntityMapping *challengeActivityMapping = [ChallengeActivity getEntityMapping:objectManager.managedObjectStore];
// all relationship mapping
[challengeActivityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"user" toKeyPath:@"user" withMapping:userMapping]];
[challengeActivityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"votes" toKeyPath:@"votes" withMapping:voteMapping]];;
[challengeActivityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"comments" toKeyPath:@"comments" withMapping:commentMapping]];
RKResponseDescriptor *challengeActivityResponse = [RKResponseDescriptor responseDescriptorWithMapping:challengeActivityMapping pathPattern:[ChallengeActivity restApiPathPattern] keyPath:@"challenge_activity" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptorsFromArray:[NSArray arrayWithObjects:challengeActivityResponse, userResponse, voteResponse, commentResponse, errorResponse, nil]];
以下是每个请求返回的 JSON 有效负载:
[{
challenge_activity: {
id: 1,
comment: "completed this task on time!",
image_url: null,
created_at: "2013-05-15T00:12:21Z",
user: {
id: 1,
name: "Kevin Liang",
image_url: <some_image_url>
},
votes: [{
id: 3,
user: {
id: 1,
name: "Kevin Liang",
image_url: <some_image_url>
}
}],
comments: [{
id: 1,
text: "first comment!",
user: {
id: 1,
name: "Kevin Liang",
image_url: <some_image_url>
}
}]
}
}]
这是获取代码:
- (void) fetchResultsFromServerAsync
{
[[WellifyObjectManager manager] getObjectsAtPath:[[ChallengeActivity restApiPathPattern] stringByReplacingOccurrencesOfString:@":challenge_id" withString:[NSString stringWithFormat:@"%d", 1]] parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
self.challengeActivities = [mappingResult array];
[self.tableView reloadData];
NSLog(@"Loaded activities");
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Hit error: %@", error);
}];
}
这是 restkit 调试输出,它显示对象在第一次 fetch 时被缓存(因为它取回数据是有意义的),而第二次 fetch 没有产生任何结果,因此没有缓存(即使调用被触发到服务器):
2013-05-19 17:41:12.844 WellifyiPhone[16147:1d903] I restkit.network:RKHTTPRequestOperation.m:154 GET 'http://0.0.0.0:3000/challenges/1/challenge_activities'
2013-05-19 17:41:12.944 WellifyiPhone[16147:1d903] I restkit.network:RKHTTPRequestOperation.m:185 GET 'http://0.0.0.0:3000/challenges/1/challenge_activities' (200 OK) [0.0999 s]
2013-05-19 17:41:12.946 WellifyiPhone[16147:21503] I restkit.core_data:RKInMemoryManagedObjectCache.m:64 Caching instances of Entity 'ChallengeActivity' by attributes 'objectId'
2013-05-19 17:41:12.951 WellifyiPhone[16147:21503] I restkit.core_data:RKInMemoryManagedObjectCache.m:64 Caching instances of Entity 'User' by attributes 'objectId'
2013-05-19 17:41:12.953 WellifyiPhone[16147:21503] I restkit.core_data:RKInMemoryManagedObjectCache.m:64 Caching instances of Entity 'Vote' by attributes 'objectId'
2013-05-19 17:41:12.955 WellifyiPhone[16147:21503] I restkit.core_data:RKInMemoryManagedObjectCache.m:64 Caching instances of Entity 'Comment' by attributes 'objectId'
2013-05-19 17:41:12.961 WellifyiPhone[16147:1d903] Loaded activities
2013-05-19 17:41:14.717 WellifyiPhone[16147:1d903] I restkit.network:RKHTTPRequestOperation.m:154 GET 'http://0.0.0.0:3000/challenges/1/challenge_activities'
2013-05-19 17:41:14.819 WellifyiPhone[16147:1d903] I restkit.network:RKHTTPRequestOperation.m:185 GET 'http://0.0.0.0:3000/challenges/1/challenge_activities' (200 OK) [0.1021 s]
2013-05-19 17:41:14.820 WellifyiPhone[16147:1d903] Loaded activities
就像我之前提到的,第一次调用我的 fetchResultsFromServerAsync 方法时,[mappingResult 数组] 返回 ChallengeActivity 的 NSManagedObject。但是,在随后的调用中,数组是空的。