核心数据 - 执行获取请求仅检索1条记录;何时插入10?

时间:2014-12-16 12:39:09

标签: objective-c core-data nsmanagedobjectcontext

在我们的系统中,user有许多accountCredits

这些accountCredits来自服务器。

我有一个请求,它在mainContext的核心数据实体的accountCredits中检索并插入10条记录,并且可以在我的Xcode控制台日志中看到这10条记录。它们似乎已成功插入。我可以在Xcode控制台日志中看到它们。

但是,之后我会执行一个非常基本的获取请求来检索用户及其各自的accountCredits - 它应该看到10;但它只能看到1,我不知道为什么。

我做的第一件事就是在每一步(前,中,后)检查contextcontext ID不会改变。所以它不是一个背景问题。

但我不知道为什么它只会看到1条记录,当我知道并记录了10条记录时。

我尝试格式化以使其易于理解并粘贴以下代码;

NSArray *accountCredits = [resource linkedResourceForKey:@"accountCredits"];
if (accountCredits) {
    for (JSONAPIResource *accountCreditResource in accountCredits) {
        VICAccountCredits *accountCredits = [VICAccountCredits accountCreditFromResource:accountCreditResource inContext:context];
        if (accountCredits) {
            [accountCredits setUser:user];
            NSLog(@"userAPI.accountCredit - %@ %lu %@", accountCredits.code, (long)[accountCredits.amountGBP integerValue], accountCredits.user.name);
        }
    }
}

NSLog(@"------");

NSLog(@"... After loading ...");

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:context];
[fetchRequest setEntity:entity];

NSError *error = nil;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

for (NSManagedObject *info in fetchedObjects) {
    NSLog(@"AccountCredits -- %@", [info valueForKey:@"accountCredits"]);
    VICAccountCredits *cr = [info valueForKey:@"accountCredits"];
    NSLog(@"cr - %@ %@", cr.code, cr.user.name);
}

accountCreditForResource像这样创建accountCredit(已编辑);

+ (VICAccountCredits *)accountCreditFromResource:(JSONAPIResource *)resource inContext:(NSManagedObjectContext *)context
{
    NSString *id = [resource safeObjectForKey:@"id"];
    VICAccountCredits *accountCredit = [VICAccountCredits objectWithRemoteID:id inContext:context];

    if(!accountCredit) {
        accountCredit = [self insertInManagedObjectContext:context];
        accountCredit.remoteID = id;
    }

    accountCredit.amountGBP = [[resource safeObjectForKey:@"amountGBP"] safeObjectForKey:@"amount"];
    accountCredit.code = [[resource safeObjectForKey:@"links"] safeObjectForKey:@"code"];    
    return accountCredit;
}

我在if (!accountCredit)中使用了断点,看到它确实输入了if语句并将其插入正确的mainContext

日志/控制台报告:

Contexts = <NSManagedObjectContext: 0x7fa86b624270> 
userAPI.accountCredit - G0CR7 1000 a d
userAPI.accountCredit - A1GH5 1000 a d
userAPI.accountCredit - A2EF7 1000 a d
userAPI.accountCredit - A4JX4 1000 a d
userAPI.accountCredit - A6NA9 1000 a d
userAPI.accountCredit - A9DI2 1000 a d
userAPI.accountCredit - D1RK0 1000 a d
userAPI.accountCredit - D2LG1 1000 a d
userAPI.accountCredit - E0UH0 1000 a d
userAPI.accountCredit - E3TU4 1000 a d
------
.. After loading ...

AccountCredits -- <VICAccountCredits: 0x7fa86b63e870> (entity: AccountCredit; id: 0xd000000000100002 <x-coredata://A4310709-6655-4333-8FE3-2C92821DA793/AccountCredit/p4> ; data: {
    amountGBP = 1000;
    code = E3TU4;
    remoteID = 548f137824848be367e1aac3;
    user = "0xd000000000040000 <x-coredata://A4310709-6655-4333-8FE3-2C92821DA793/User/p1>";
})
-> 112 cr - E3TU4 a d

我发现这很难理解为什么在插入10条记录时它只能看到1?

有人能说清楚这个吗?

我确实检查了context ......它永远不会改变。

我甚至尝试删除该应用,并重新启动/清除所有数据的模拟器。


原来它缺少核心数据中的正确关系。谢谢所有帮助过的人。

1 个答案:

答案 0 :(得分:-2)

试试这个

//set properety for array with data

@property (strong) NSMutableArray *data;


NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Entity"];
    self.data = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

别忘了将此方法放在视图控制器中:

- (NSManagedObjectContext *)managedObjectContext
{
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}