Restkit fetching empty results on subsequent fetches

此生再无相见时 提交于 2019-12-08 06:11:04

问题


I am facing a really weird problem and I hope someone can help me.

I am using Restkit (0.20) with CoreData setup, and I have all the attribute/relationship mapping setup for responseDescriptor. This is set correctly because when I call getObjectsAtPath:parameters the first time, I got the correct results back and it was able to map to the correct objects.

However, on subsequent getObjectsAtPath calls, the mappingResult yields empty results for some reason, even though I can see the request coming through from server just fine, and the same resulting JSON is sent back to client.

Has anyone seen this problem??!?!!?

Here is the mapping code:

 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]];

Here is the JSON payload that is returned on each request:

[{
  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>
      }
    }]
  }
}]

Here is the fetch code:

- (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);
    }];
}

Here is the restkit debugging output, which shows objects being cached on first fetch (makes sense as it got data back), while the second fetch yielded nothing and thus no cache (even though the call was fired to the server):

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

Like I mentioned before, the first time my fetchResultsFromServerAsync method is called, [mappingResult array] returns an NSManagedObject of ChallengeActivity. However, on subsequent calls, the array is empty instead.

来源:https://stackoverflow.com/questions/16632743/restkit-fetching-empty-results-on-subsequent-fetches

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!