9

I have an app that records and stores accelerometer data as core data managed objects. I would like to include some sample saved recordings in the simulator to test the app on different platforms, since the simulator cannot record from the accelerometer. I have successfully included *.xcappdata files in the build scheme and it works on devices, but simulator fetches come back empty.

Apple documentation seems to indicate that including app data in the simulator is possible but I have not gotten it to work. I have tested including data saved from one device on another device and that works fine. I am getting no error message, just an empty fetch.

I know I could include data in the form of text files in the app, read and format them upon launch, and save them as managed objects, but it would create a bunch of appendix code and files in the project, and I would rather work more cleanly.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
AlexMath
  • 567
  • 1
  • 6
  • 16

2 Answers2

6

You can find your application's data container path by running xcrun simctl get_app_container <device> <bundle identifier> data in a Terminal window. Device can be the name (quoted) or the UDID (see xcrun simctl list). For this to work, the relevant simulator will have to be running, and have a version of the app installed on it.

Right-click on the .appdata file to show the contents of package. Copy the core data files you got from Folders in AppData/* into the analogous directory then start your app in the Simulator.

Xcode does not currently have a GUI to manage containers in the Simulator like it does for physical devices. If you don't mind please file an enhancement request at https://bugreport.apple.com/ requesting this in the Devices & Simulators window.

AlexMath
  • 567
  • 1
  • 6
  • 16
russbishop
  • 16,587
  • 7
  • 61
  • 74
  • To attempt this solution, I figured some minor clarifications: 1. You have to already have installed your app on the relevant simulator (otherwise the data container does not exist). 2. The relevant simulator must be running (otherwise it returns Shutdown error). 3. The commands are run in Terminal. It did not seem to work for me, though. I correctly identified the data container, put the .appdata in it (the folder changes every installation but the file gets moved). I included the data in the scheme. Still no data in the app. – AlexMath Jan 02 '18 at 17:38
  • BTW, I did file a request with Apple as per your suggestion. The ID is 36256124. – AlexMath Jan 02 '18 at 17:43
  • Thanks for filing the request. You probably need to take the contents out of the .appdata folder (eg the files in Documents or Caches) and put it in the container directory for your app in the Simulator. You can't just drop the "appdata" bundle in there. – russbishop Jan 08 '18 at 20:57
  • Relevant files were in the AppData/Documents folder of .appdata contents and were called Model.sqlite, Model.sqlite-shm, Model.sqlite-wal. – AlexMath Jan 09 '18 at 22:05
5

I've just figured it out that, as a workaround at least for Xcode 9.2/iOS 11.2 simulator, you can set (HOME/CFFIXED_USER_HOME) (I'm not sure whether of those actually works) environment variables in launch arguments to a path to .xcappdata/AppData, like shown below (I used SRCROOT relative path just for illustration of possibilities - it works with any other kind of path like absolute ones and etc). Beware that the changes in the app data will be reflected right in that folder, so if you don't want that, it makes sense to point it to the copy.

Environment for .xcappdata

Grigory Entin
  • 1,617
  • 18
  • 20
  • I'm having no luck with this on Xcode 12.3. Any ideas? – CommaToast Mar 26 '21 at 15:52
  • 1
    It does not work there. But you can try employ `xcrun simctl install_app_data` to install the data (*after* you have your app installed in the simulator) - at quick glance it works? See more info here: https://github.com/lionheart/openradar-mirror/issues/6578#issuecomment-808282851 – Grigory Entin Mar 26 '21 at 16:23
  • OK so lets say I were to use `xcrun simctl install_app_data`... how can I set which .xcappdata to use based on which .xctestplan is selected? Seems to me that a build script and pre-test action both have no knowledge about which test plan is active. So short of making a separate scheme for every .xcappdata, what can we do...? – CommaToast Mar 30 '21 at 00:05