Core data works in one simulator but not in others, or in real device.

A novice here! I have an app that stores some lists of strings and numbers to coredata. It works perfectly in the simulator for my iPad but when I connect to the device itself the data is not found. Nor can it be found in another simulator. Please can someone explain how to export the datamodel to the real device?
Thanks in advance!

David C

Accepted Reply

David,


it sounds like you're describing the following situation:


(1) you run your App in a simulator. initially, there is no data (unless you've included some on app setup), but your App can then create and update data as it runs, and the modified data is persisted using CodeData.


(2) as you run your App repeatedly on the same simulator, you have access to the data as created and modified from the previous run(s).


(3) you run your App on a physical device and you see no data that was used in the simulator.


(4) you run your App on a different simulator and see no data in your iPad simulator.


this is correct behavior. CoreData is persisted on disk, on the device on which it's running. (you determine where it is persisted in creating the CoreData stack.) each simulated device is effectively a different device, as is any physical device you might use.


if you're asking about how to create and modify data stored using CoreData in a simulator or physical device and then carry that over to other simulators or physical devices, that has a complicated answer. one possible option is to synch devices using iCloud.


if you're asking how to pre-populate a CoreData database in your App so that it is non-empty when installed on a device and run for the first time, there are several methods.


if the database is small, you can write code that runs during startup: once the CoreData stack is set up and you recognize the data store is empty, create initial objects directly in code. if the database is large, well, that's another thing.


J.

Replies

Maybe look at some other alternatives. Core data is one of the most complex and subtle data storage techniques ever devised. If you just have static data, put it in a plist and be done with it. If you truly need a local database, use SQLite directly.

Thanks for this John, and I may well yet have to follow your advice. But it would be a pity as the data I ultimately wish to store has quite a bit of structure, so CoreData would seem appropriate. It’s particularly frustrating because, using the Device Inspector, I can now see that the sqlite data is present in the bundle on the iPad: it is just not being accessed. I’m not afraid of complexity – I’d just like to understand what is going on :-)

David,


it sounds like you're describing the following situation:


(1) you run your App in a simulator. initially, there is no data (unless you've included some on app setup), but your App can then create and update data as it runs, and the modified data is persisted using CodeData.


(2) as you run your App repeatedly on the same simulator, you have access to the data as created and modified from the previous run(s).


(3) you run your App on a physical device and you see no data that was used in the simulator.


(4) you run your App on a different simulator and see no data in your iPad simulator.


this is correct behavior. CoreData is persisted on disk, on the device on which it's running. (you determine where it is persisted in creating the CoreData stack.) each simulated device is effectively a different device, as is any physical device you might use.


if you're asking about how to create and modify data stored using CoreData in a simulator or physical device and then carry that over to other simulators or physical devices, that has a complicated answer. one possible option is to synch devices using iCloud.


if you're asking how to pre-populate a CoreData database in your App so that it is non-empty when installed on a device and run for the first time, there are several methods.


if the database is small, you can write code that runs during startup: once the CoreData stack is set up and you recognize the data store is empty, create initial objects directly in code. if the database is large, well, that's another thing.


J.

I'd ignore the simulators in this example, and focus on it not working on a device. In other words, don't try to troubleshoot this issue based on what does/doesn't happen on any simulator. Put your efforts into what is/isn't working on a device, same as your users.


As DMG notes, each sim build uses a different path/location, yet again different from a device build, and as John noted, CD is complex, requiring a specific pre-existing skill set.


If your goal is to learn, then head on - if your goal is to get an app in the store, now, then consider using an alternative data source (plist is most basic) that presents lower barriers, allowing you to get your app to users sooner, then re-visit effort a CD solution down the road as the situation warrants and your skills expand.


>...how to export the datamodel to the real device?


More like preload & import. Although dated (uses older tools, etc.), this RW link should shed light:

h ttps://www.raywenderlich.com/2935-core-data-on-ios-5-tutorial-how-to-preload-and-import-existing-data


Good luck.

Thanks very much - this is extremely helpful. Together with the reply from KMT I think I now understand the path forwards!