Core Data doesn't save the first time - ios

I have a problem with RestKit 0.22 and Core Data for saving. To be sure that the rest of my project doesn't cause the problem, I reproduce it with RKTwitterCoreData in RestKit examples. I simply remove seeding part to start with an empty sqlite database. My app is based on the same concept.
My problem happens the first time I run the app. The app receives the data from twitter and displays it. But if I look at the sqlite database itself, it's empty. When I start the app the second time, the database is filling.
Is it possible for RestKit to save the data at the moment it receives it?
Is it possible that RestKit cache all data before saving them. I though that my problem happened because managedObjectCache in RKManagedObjectStore is set with RKInMemoryManagedObjectCache but I also tried with RKFetchRequestManagedObjectCache and I got the same result.
I also tried to save manually in the success bloc with [[[RKManagedObjectStore defaultStore] persistentStoreManagedObjectContext] save:nil] and that didn't work.
Thank you

RestKit does save the mapping result automatically, and before calling the success completion block.
If you want to check what is in the store, ask the MOC (main thread or persistent store MOC) and check the registeredObjects / run a fetch request.
You should not look at the SQLite file(s). The table structure is a private implementation detail, and if you load the file at the command line with the wrong options you won't necessarily see what you expect.

Related

Core data context change causes crash

I have some Peoples profile, which I am saving in Core data. I receive them from server.
Next time when I run app, old data exists there, and I call a service call, which get more profiles including this.
I remove all the profiles and insert new.
When I try to work on existing screen, it crashes. I am pretty sure, that I changed database, and that object no more exists. While in reality, that object is inside CoreData but with another context.
Now, how can I improve this situation, that if I remove all Data and insert same data again, it shouldn't crash with existing data.
Let me know if anything wrong in Question, or I need to explain more.
Thanks.
If problem is really in "While in reality, that object is inside CoreData but with another context.", you can call the same object from needed context with it's NSManagedObjectID.
So just save your ids and pass them around, and when you need exact objects, call them from needed context with these ids

Isn't NSPersistentStore thread safe?

I am downloading json data from server, parsing it and storing it in to the database using CoreData. So next time when the view is loaded, it fetch the data using Core Data and along with that parsing and db insertion also happens. Before insertion, I am deleting the existent data also. Following is the flow that I am using:
Checks for data in database.
If available, fetch and render data in UI. And downloading data from server is also do along with fetching and rendering.
Delete the existing data and insert again.
The above steps crashes the application some time. What is the best approach to handle this case?
What I felt, sometimes at the time of fetching, download completes and deletes data. It may be depends on the speed of fetching.
That's why I got the question in mind that NSPersistentStore is thread safe?
Can anyone help to understand what approach to follow in this case?
Sree.

Core data is not persisting in IOS

I'm having an issue where my core data does not persist sometimes. This is the scenario I'm running. I run the app when I'm not logged in, login, and the app stores some data. I then run the app again, this time as the logged in user, and pull that data. It was all working well until I tried to run the app as if it had been downloaded for the first time. To do this I deleted the app from my phone and ran the above steps. However now no data is pulled from the Core Data. Here are some interesting notes and things I have done to remedy the situations.
1) I know with 100% certainty that the data persists if the app is not being run as if just downloaded.
2) The data occasionally persists when run as if just downloaded, but the minority of the time.
3) When I run the app as if just downloaded and store the data into the Core Data, I do an immediate check to see if the data was stored and it is. When I run it again, however, the data is gone.
I'm refraining from posting code right now just because it's very large and would take a lot of time to shorten for the sake of posting. However if you think it'll be helpful in light of what I have said I will post it.
You state that you save with [document saveToURL:forSaveOperation].
In order to persist Core Data you have to save the context.
[managedObjectContext save:nil];
Make sure you call this with a valid context before you expect your data to be persisted. I suspect you have this in some places but not in all required places. One way to find out is to put a breakpoint on each save statement and check if the saves are called as expected.

Dealing with Managed Objects between two MVC's with one database

Current Setup:
MVC-1, onViewDidLoad, creates a Managed Object, fetches data from the Managed Object and updates various UI elements.
MVC-2: Same exact setup. I copied and pasted the same code from MVC-1 into MVC-2.
Issues I am seeing:
After launch and opening MVC-1, all the code is executed without any errors. The ManagedObject is created, the fetch requests on the ManageObjectContext work and the UI is properly updated. However, when switching to MVC-2 it seems that none of the data, that MVC-2 is suppose to fetch, is actually being updated. All the UI Elements in MVC-2 have the same data from the last time the application was launched.
Furthermore, if I launch the application and open MVC-2 first I get the same results, only MVC-1 does not appear to be updated this time around.
What is it that I am over looking? I have explicit saves to the database being made. I am (at least I think I am) creating two different ManagedObjects. Do I need to somehow close one before creating the other? Any advice would be very helpful. Thanks.
Figured it out.
I was saving the MOC, however I was not writing the NSManagedDocument to disk (I am using a ManagedDocument as a container for the Core Data database).
So naturally, when I switched to the MVC-2 I would not have the updated data.
I now save the MOC, write the MD to disk and close the MD at viewWillDisappear.
Thanks for the help gusy.

Core Data delay when switching NSPersistentStore files

I'm developing an app with Core Data that periodically downloads all the data from a webservice. Since the download can fail or be cancelled by the user, I want to be able to roll back to the previous state. I tried undoing the NSManagedObjectContext, but that seemed a bit slow (I have tens of thousands of entities). What I'm doing right now is making a backup of the persistent store file, download the data, and, if the download fails, replace the store file with the backup. This seems to work correctly, except there seems to be a delay after I can fetch entities from the store: if after the download I go immediately to a UITableView that uses an NSFetchedResultsController, I find it empty. If I wait some seconds, everything is ok.
So my question is: has anyone had this kind of delays too? Is there something that can be done to avoid this problem, something that forces everything to be ready, even if it blocks the thread?
I haven't used this setup but I think the delay you are seeing is probably caused by Core Data having to clear all it's caching. Core Data uses If you use a cache with the fetched results controller it will have to test and then delete it's existing cache.
I think the best thing to do is to tear down you Core Data stack and reboot it from scratch. That includes recreating a fresh fetched results controller.

Resources