magicalpanda / MagicalRecord

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

UI Freezes on scrolling tableview while importing data with MagicalRecord 2.3 #1286

Open deepakdhanaie opened 7 years ago

deepakdhanaie commented 7 years ago

I'm using MR 2.3 version while writing data to the database using MR as bridge and simultaneously scrolling the CollectionView in which I'm showing the data, its shows a lag on scrolling and sometimes UI freezes. I'm using swift 2.3 version.

zoroD commented 7 years ago

I'm facing the same issue. Are you using Xcode8.x and iOS10.x?

deepakdhanaie commented 7 years ago

Yes I'm using Xcode 8.2.

zoroD commented 7 years ago

What I found out for now is NSManagedObjectContext save very slow on iOS10 device builded by Xcode8.x. Here's some detail: 1.My MR version is 2.3.2. 2.I build my app with same code by Xcode7.3 and Xcode8.2. All works fine on iOS9. But on iOS10, the two builds perform different. The build which builded by Xcode7.3 works fine but the other is very slow. 3.I profile my app and find out the line, saveResult = [self save:&error], in NSManagedObjectContext (MagicalSaves) .m file take too much CPU on background thread. It blocks main thread and UI. 4.I use [MagicalRecord saveWithBlock:block completion:completion] method to save data on background thread. Are your situation same as mine? Can you give more detail about your issue? And if anyone need some more detail. I am very willing to provide. I just don't know what detail I should provide for now.

deepakdhanaie commented 7 years ago

Yes exactly, saving to db takes time, i have 30K records and as the records are increasing , time to save them in db is increasing too and also there lot of lag while saving on tableview/Collection, i have profile the same thing "saveResult = [self save:&error]" on this line. I have read a lot of blogs but found no solution, if magical record is using different context for saving, why its causing lag then. And yes I'm using the same saveWithBlock:block method to save on background thread and NSFRC to read data on default context.

zoroD commented 7 years ago

It looks like we are facing the same problem. In my case, I have a User object which have a one-to-many relationship with Log object. With more Log for one User, insert a new Log to User will be slower. In my test db, there are 70k+ Logs for one single User. And the time for insert one new Log to User will take about 3 to 5 seconds. I'm not sure if this is related to SDK or Compiler's update for iOS10 or Xcode8. Do you test on iOS9 or build by Xcode7.x?

deepakdhanaie commented 7 years ago

I ddnt test that on iOS 9 and i have upgraded my devices to iOS10, i need to find one, Yes i have array of recipes with two or more one-to-many relationship, the saving time is ok when saved records are below 10k but now i'm having 30k records so its sometime takes 10 - 15 seconds for saving the data.I have upgraded my project to Xcode8.x but i was facing the same issue in Xcode7.x too. I really. Magical Record has provided the upgrade for MR3.0 but one has to provide its path in pod file you may try with that, i cant use that one because i have intensively user MR 2.3.2 and MR 3.0 has my syntactical changes.

zoroD commented 7 years ago

I don't know if MR3.0 can fix this problem. Even if it can, I can't upgrade to MR3.0 as my workmate tried once and gave up with many errors. I don't have much time to dealing with those errors right now and I can't list all the test case -_-. I know if saving a child context with a parent context on main thread will block both context and UI in theory. And I know there must be something using or blocking main thread. I just can't prove it by instrument or figure out what is massing up the main thread. Do you have any thought about that?

deepakdhanaie commented 7 years ago

I have tried many ways to get rid of that even tried rootContext for showing the results on UI then it was smooth but you cant use that, as this is voilates apple Multithreading. I followed one of the blog that you can also use other context for NSFRC but it causing inconsistency. I really dnt know what blocking main thread here but MR should be able to handle all this stuff smoothly and its not doing so. I read this block "http://quellish.tumblr.com/post/93190211147/secrets-of-nsfetchedresultscontroller-happiness"

zoroD commented 7 years ago

OK. I may have to try to use NSMOC to manage data but not through MR. Maybe I can figure out if this is a MR issue. And I read some useful articles: http://floriankugler.com/2013/04/29/concurrent-core-data-stack-performance-shootout/ https://www.cocoawithlove.com/2008/03/testing-core-data-with-very-big.html http://stackoverflow.com/questions/29900193/core-data-parent-context-blocks-child Hope these may help you. I'll let you know if I make any progress.

sreenureddy commented 7 years ago

I am also facing same issue and My App is crashing at this point

(BOOL) MR_truncateAllInContext:(NSManagedObjectContext *)context if you know any solution please help to me.

zoroD commented 7 years ago

I have something to share. I have done a lot of test. It turns out if I use [MagicalRecord saveWithBlock:block completion:completion] to save data on background thread, the save is very slow. In my case it is about 2 to 5 seconds to save a single Log with 70k nested Logs for a User. My fix is not using MR but creating a new MOC whose parent is a new PSC. The new MOC and PSC is separated from MR's default MOC and default PSC. I got 40 to 100 milliseconds to save a single Log with same nested Logs for a User as before. UI is smooth and even better than before. I don't know if this is a good way to fix this issue. And I have many things to do like to marge data to those MOC created by MR to make sure other part of my app will get those logs saved by the MOC which created by my new code. Even don't know if marge data could be a new problem. Can anyone help with this? @sreenureddy I didn't use MR_truncateAllInContext before. I don't delete data from any MOC in my app. So I'm afraid I can't help you.

deepakdhanaie commented 7 years ago

I tried upgrading to Magical Record 3.0 and batch of 20 objects takes less than 1 sec, its fast but theres a duplicate issue which i found and every entity i'm having duplicate records.