magicalpanda / MagicalRecord

Super Awesome Easy Fetching for Core Data!
Other
10.8k stars 1.79k forks source link

App Freeze when delete a entity with many relationships (cascade delete) #1242

Closed hei9gag closed 8 years ago

hei9gag commented 8 years ago

http://stackoverflow.com/questions/37787092/ios-app-freeze-when-delete-entity-with-magical-record

I am using MagicalRecord framework to “clean up” Core Data code. When the app launch or resume from background, the program will try to clean up some data with the following code:

[MagicalRecord saveWithBlock:^(NSManagedObjectContext localContext) { NSDate date = [[NSDate date] dateByAddingTimeInterval:-259200];

NSFetchRequest *request = [NGCOwner MR_requestAllSortedBy:@"updatedTime" ascending:YES withPredicate:[NSPredicate predicateWithFormat:@"updatedTime < %@", date]];

NSArray *results = [NGCOwner executeFetchRequest:request inContext:localContext];

NSInteger count = MAX(0,[results count]-100); //do not delete the lastest 100 results

for (NSInteger i = 0; i < count; i++) {
    NGCOwner *owner = [results objectAtIndex:i];
    if([userId isEqualToString:owner.userId]){
        continue;
    }
    [localContext deleteObject:owner];
}

}]; The entity "NGCOwner" has 17 relationships and set the delete rule to Cascade. When the program delete the NGCOwner entity (approx. 170 NGCOwner), it makes the NSPersistentStoreCoordinator busy and freeze the app.

Can I force NSPersistentStoreCoordinator not to execute in main thread?

rex3du commented 7 years ago

Hi, I have the same issue. Can you tell me why you closed it? Have you found any solutions?