Monday, 9 September 2013

MagicalRecord import only saving last item

MagicalRecord import only saving last item

I have an AFNetworking call that is calling my API and storing the
results. I have a similar call that is working just fine this way. However
for this one it seems to only be storing the last item.
[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];
}];
When I do it with a context, it doesn't even save the items. I am sending
a notification when the receiving of this information is complete and in
that notification I am displaying the results. Only one item is found with
a [CustomerFields findAll].
Array1 shows the complete list of items, but once I get back to the other
controller it only returns the very last item that was in the array. Also
if I wrap this in a saveWithBlock it will not see any items in the other
controller.
Why is it only showing the last record from the import when I do a findAll?

No comments:

Post a Comment