I’m using Magical Record library. Its cool but I have some trouble.
My second method return incorrect data. First and second methods must return the same quantity of quotes. Does findAllWithPredicate work properly?
UNLOCK AND FETCH QUOTES
-(NSArray*) unlockAndFetchQuotes
{
CoreDataManager *instance = [CoreDataManager instance];
[instance unlockUnitNumber:1];
return [instance fetchQuotes];
}
UNLOCK UNIT NUMBER
-(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];
}
}
FETCH QUOTES
-(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;
}
First and second methods must return the same quantity of quotes. The second method doesn’t work properly.
My output
___ must be unlocked quotes 500
___ unlocked quotes by first method 500
___ unlocked quotes by second method 250
total unlocked array 250

UPDATE2
I found a problem. Firstly I do unlockAndFetchQuotes. I trace this function and found something strange. Saving to coredata goes after fetching quotes. Now I’m looking for soluions to save instantely.
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 ***
I unlocked my unit and fetched quotes.
saveNestedContexts is async method. I need to use another method