magicalpanda / MagicalRecord

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

(iOS 12 beta 2) Multiple call of "MagicalRecord saveWithBlock:" freezes UI #1331

Open ghost opened 6 years ago

ghost commented 6 years ago

Hello,

In iOS 12 beta 2 I face the following issue.

I call from UI a method to update some data in my database with the following code:

[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
    Item *theItem = [FriendItem MR_findFirstWithPredicate:aPredicate inContext:localContext];
    [theItem setSomeData:data];
}];

The problem is when this function is called multiple times, the app freezes.

The console output is:

Created new private queue context: <NSManagedObjectContext: 0x281da8000>
Created new private queue context: <NSManagedObjectContext: 0x281d992c0>
→ Saving <NSManagedObjectContext (0x281da8000): saveWithBlock:completion:> on a background thread
→ Saving <NSManagedObjectContext (0x281d90960): MagicalRecord Root Saving Context> on a background thread

That was working for 2 years. Is something that I missing, or there are changes in core data in iOS 12?

Τhanks in advance.

laGrave commented 5 years ago

Same issue on beta 3

laGrave commented 5 years ago

As I can see, code stuck at NSManagedObjectContext func "save:"

fysteven commented 5 years ago

Is this still an issue in the latest iOS 12 beta?

s-gilroy commented 5 years ago

Running the latest iOS beta I'm having the same issue.

@laGrave @fatourosfotis Did either of you ever figure out a workaround for this?

s-gilroy commented 5 years ago

To close the loop for anyone that is experiencing this issue. Seems like this is a bigger issue than Magical Record (https://forums.developer.apple.com/message/324460#324460).

Our temporary solution was to make the root context a main queue concurrency type and to have MR_defaultContext to return the root context instead. It's a bad fix but at least no more UI deadlocks.

laGrave commented 5 years ago

I've stuck with that issue and decided to quickly migrate to the Realm DB. I'll try the method you suggested

tuyenbq-0446 commented 5 years ago

i'm facing the same issue as you said but not sure why did happen anyway. Please tell me the reason if possible. thx

AKyashkinCherryhome commented 5 years ago

Is there still no solution yet? I'm facing the same issue with three simple "SaveWithBlockAndWait" calls. It can handle two simultaneously calls, but it fails on three calls, which looks really odd to me.

yaoqimin2013 commented 5 years ago

I'm facing the same issue, since changing the rootContext to a mainContext is not a good solution, I have to look for an alternate solution. Any advice >.<

AKyashkinCherryhome commented 5 years ago

This is what I basically did. I remade all my operations to work in synchronous mode in one background thread, which is fixed my issue, since all concurrent writings are gone. Not sure if that's a good one, probably will migrate to other DB as well.

// "global" var, stored in singleton Manager
dispatch_queue_t dispatchQueueSync = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

// sync write operation
dispatch_sync([[DataManager shared] dispatchQueueSync], ^{
    [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
        // DB opertions
    }];

    dispatch_async(dispatch_get_main_queue(), ^{
        // refresh UI and stuff calls
    });
});
shivam-ucreate commented 5 years ago

Having the same issue in iOS 12 @AKyashkinCherryhome the above solution didn't work.