0

我有一个 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 显示了完整的项目列表,但是一旦我回到另一个控制器,它只会返回数组中的最后一个项目。此外,如果我将其包装在 a 中saveWithBlock,它将不会在另一个控制器中看到任何项目。

为什么当我执行时它只显示导入的最后一条记录findAll

4

2 回答 2

0

我对 Magical Record 不太熟悉,但也许您没有保存核心数据上下文?

我假设该方法MR_importFromArray:将记录插入CoreData?

如果不是这种情况,您可能想要遍历您的结果并一一创建它们?

[client getPath:@"GetItemsByFilter/" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject)
{
    NSArray *customerFieldResults = [responseObject valueForKeyPath:@"details.items"];

    customerFieldResults = [CustomerFields MR_importFromArray:customerFieldResults];

    //loop through array and create records

    //save your core data context here

    [[NSNotificationCenter defaultCenter] postNotificationName:kCustomerFieldSetComplete object:nil];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error){}];
于 2013-09-09T17:04:45.500 回答
0

我想到了。事实证明,我使用的 API 发生了变化,并且我使用的字段relatedByAttribute返回空白(一个错误),所以它认为我的所有记录都是重复的并覆盖它们。

于 2013-09-09T18:10:26.247 回答