1

我正在使用 Magical Record 库。它很酷,但我有一些麻烦。我的第二种方法返回不正确的数据。第一种和第二种方法必须返回相同数量的报价。findAllWithPredicate 工作正常吗?

解锁并获取报价

-(NSArray*) unlockAndFetchQuotes
{
 CoreDataManager *instance = [CoreDataManager instance];
 [instance unlockUnitNumber:1];
 return [instance fetchQuotes];
}

解锁单元号

-(void) unlockUnitNumber:(int) number
{
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"identificator=%d", number];
 NSArray *array =[CDInApp findAllWithPredicate:predicate];
 if (array.count>0)
 {
    CDInApp *inApp = [array objectAtIndex:0];
    inApp.isLockedValue = NO;
    [[NSManagedObjectContext defaultContext] saveNestedContexts];
 }
}

获取报价

-(NSArray*) fetchQuotes
{
 int z=0;
 NSArray *arr = [CDQuotes findAll];
 for (CDQuotes * quotes in arr)
 {
    if (!quotes.inApp.isLockedValue)
    {
        z++;
    }
 }
   NSLog(@"_____ unlocked quotes by first method %d", z);


 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"inApp.isLocked != 1"];
 int count = [CDQuotes countOfEntitiesWithPredicate:predicate];
   NSLog(@"_____ unlocked quotes by second method %d", count);

 NSArray *array = [CDQuotes findAllWithPredicate:predicate];
   NSLog(@"total unlocked array %d", array.count);

 return array;
}

第一种和第二种方法必须返回相同数量的报价。第二种方法不能正常工作。

我的输出

___ must be unlocked quotes 500
___ unlocked quotes by first method 500
___ unlocked quotes by second method 250
total unlocked array 250

在此处输入图像描述

更新2

我发现了一个问题。首先我做unlockAndFetchQuotes。我跟踪这个函数,发现了一些奇怪的东西。获取报价后保存到 coredata。现在我正在寻找可以立即保存的解决方案。

2012-12-21 18:13:32.992 CoreBug[6713:11603] _____ unlocked quotes by first method 8
2012-12-21 18:13:32.994 CoreBug[6713:11603] _____ unlocked quotes by second method 6
2012-12-21 18:13:32.996 CoreBug[6713:11603] -[NSManagedObjectContext(MagicalSaves) MR_saveWithErrorCallback:](0x7464700) -> Saving <NSManagedObjectContext (0x7464700): *** DEFAULT ***> on *** MAIN THREAD ***
2012-12-21 18:13:32.997 CoreBug[6713:15603] -[NSManagedObjectContext(MagicalSaves) MR_saveWithErrorCallback:](0x8148ef0) -> Saving <NSManagedObjectContext (0x8148ef0): *** BACKGROUND SAVING (ROOT) ***> on *** BACKGROUND THREAD ***

我解锁了我的单位并获取了报价。

4

3 回答 3

1

isLockedValue在循环中使用,而不是isLocked. 据推测,isLockedValue不存在并且将永远存在nil,导致循环中的所有项目都被计算在内。

相比之下,您的谓词似乎工作正常。

于 2012-12-20T11:39:35.237 回答
0

您是否尝试过将谓词更改为

[NSPredicate predicateWithFormat:@"inApp.isLocked = NO"]

?

于 2012-12-20T19:32:41.213 回答
0

saveNestedContexts 是异步方法。我需要使用另一种方法

[localContext MR_saveNestedContextsErrorHandler:nil completion:^{
        [self fetchQuotes];
    }];
于 2012-12-22T18:25:58.830 回答