xain ul abidin
xain ul abidin

Reputation: 9

MagicalRecord saving takes time

i am using magicalrecord in my app to manage coredata, every thing is perfect now but i am having an issue,i created 10k entities using this code:

[MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext * _Nonnull localContext) {

Person *person = [Person MR_createEntityInContext:localContext];
person.organization = [Organization MR_findfirstInContext:localContext]; 
person.name = name;
person.id  = id;
person.age = age;

]};

after creating 10k records when i perform any change in database using magicalrecord it gets stuck for 4 or 5 seconds while saving to root context, which makes UI freez no matter if its on main thread or background thread.

Edited: i have found the problem if i remove relation (which is organization) from save block it dont get stuck, if i fetch organization from out side the block and assign in inside the save block it throw an exception that organization is created in different context.

Upvotes: 0

Views: 165

Answers (1)

Mistrx丶
Mistrx丶

Reputation: 267

//case A
[MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {

    NSLog(@"current thread is main thread %d and thread is %@", [NSThread isMainThread], [NSThread currentThread]);

    //some ManagedObject code
    ...
}];

//case B
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {

    NSLog(@"current thread is main thread %d and thread is %@", [NSThread isMainThread], [NSThread currentThread]);

    //some ManagedObject code
    ...
}];

test this code, you will find case A always run at the MainThread,if you have 10K entities to store, the UI get stuck.

enter image description here

Look at this https://github.com/magicalpanda/MagicalRecord/blob/master/Docs/Saving-Entities.md

Upvotes: 0

Related Questions