【发布时间】:2013-09-09 16:01:22
【问题描述】:
我有一个 AFNetworking 调用,它正在调用我的 API 并存储结果。我有一个类似的电话,以这种方式工作得很好。但是对于这个,它似乎只存储最后一项。
[client getPath:@"GetItemsByFilter/" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Operation: %@", operation);
NSLog(@"Response: %@", responseObject);
NSArray *customerFieldResults = [responseObject valueForKeyPath:@"details.items"];
NSLog(@"Results: %@", customerFieldResults);
__block NSArray *array;
@try {
[CustomerFields MR_truncateAll];
array = [CustomerFields MR_importFromArray:customerFieldResults];
NSLog(@"done setting array: %@", array);
} @catch (NSException *e) {
NSLog(@"Exception: %@", e);
} @finally {
NSLog(@"tc done");
}
NSLog(@"Array 1: %@", array);
[[NSNotificationCenter defaultCenter] postNotificationName:kCustomerFieldSetComplete object:nil];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Operation: %@", operation);
NSError *jsonError;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[[error localizedRecoverySuggestion] dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&jsonError];
NSLog(@"Error: %@", [error localizedRecoverySuggestion]);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Api Error: %@", [dict valueForKey:@"status"]] message:[dict valueForKey:@"statusMessage"] delegate:nil cancelButtonTitle:NSLocalizedString(@"Ok", @"Okay button") otherButtonTitles:nil];
[alert show];
}];
当我使用上下文执行此操作时,它甚至不会保存项目。收到此信息完成后,我将发送通知,并在该通知中显示来自[CustomerFields findAll] 的结果。仅找到一项带有 [CustomerFields findAll] 的项目。
Array1 显示了完整的项目列表,但是一旦我回到另一个控制器,它只会返回数组中的最后一个项目。此外,如果我将其包装在 saveWithBlock 中,它将不会在另一个控制器中看到任何项目。
为什么当我执行findAll 时它只显示导入的最后一条记录?
【问题讨论】:
标签: ios objective-c afnetworking magicalrecord