auto-populate Documents directory at build time

I’m sure I’m missing something obvious in the docs, but I can’t figure out how to automatically populate the Documents directory when I build my app.

I have a large file that WILL CHANGE (ie can’t be in Resources directory) and so I need it to always be in the documents directory at first launch.

All tips appreciated but I’m hoping you don’t advise me to copy it from the Resources Directory at start-up because the I/O constraints will exceed the allowed app init time on the iPhone…and it would be a wasteful complication to boot…

Thanks
Dewey [import]uid: 6175 topic_id: 4704 reply_id: 304704[/import]

Hi Dewey,

The Documents folder doesn’t exist until the user installs the app on the device. You’ll have to copy the file from the Resources directory to the Documents directory the first time the user launches the app.

However, I wouldn’t start copying anything over or performing any heavy lifting at init time because, as you know, the OS would kill your app for taking to long to start. You can get around this by giving your app the chance to fully load and then initiate any in-depth processes in a subsequent frame. You can do this by using an event listener:

--this function does a lot of the heavy lifting  
local function loadData()  
 --kill the event listener since this function only needs to run once  
 Runtime:removeEventListener("enterFrame", loadData)  
  
 --perform heavy lifting  
 --[heavy lifting code goes here]--  
end  
  
--this function sets up the app and triggers the heavy lifting function  
local function init()  
 --perform some basic setup, such as placing the nav, tab bar, etc.  
 --[setup code goes here]--  
  
 --trigger the heavy lifting function  
 Runtime:addEventListener("enterFrame", loadData)  
end  
  
--now start the program  
init()  

You may also want to throw in a function before all of this that checks to see if this is the first time the app has run and if not to run a check for your data file, and if the data file is missing, reload it (because the user could have also deleted your data from the Documents folder directly. They have access to it in iTunes and can do that.)

The ATA app is a good example of an app that checks for and restores a previous state. Take a look at the main.lua file: https://github.com/gg-ansca/ATA/blob/master/main.lua

The other option to using event listeners is to use timer.performWithDelay().

local function loadData()  
 --perform heavy lifting  
 --[heavy lifting code goes here]--  
end  
  
local function init()  
 --perform some basic setup, such as placing the nav, tab bar, etc.  
 --[setup code goes here]--  
  
 --trigger the heavy lifting after a few milliseconds   
 timer.performWithDelay(50, loadData )  
end  
  
--start the program  
init()  

-Gilbert [import]uid: 5917 topic_id: 4704 reply_id: 15103[/import]

I will add one more thing. I just filed case# 2585 as a feature request to give developers access to the app Library folder. The Library folder may provide a way to ship something like a writeable database with your app, rather than copying it to the Documents folder. I haven’t had a chance to verify this yet. Be aware that we have to take a look at any cross-platform compatibility issues before implementing something like this.

-Gilbert [import]uid: 5917 topic_id: 4704 reply_id: 15106[/import]

I’ve gotten reports that IOS does not support the auto-population of a WRITABLE directory at app-install time.

At the same time, I’m told that IOS will abort your app if the start-up process takes too long. (like if you copy a large file from resources to documents)

Are there any X-code guys out there that can confirm this catch-22??

If you bundle a large writable DB with your app, how do you get it into the documents directory within the startup time limits imposed by IOS?

All information appreciated…

[import]uid: 6175 topic_id: 4704 reply_id: 15584[/import]

I think Gilbert answered your question above. Set the code that will be doing the copying/db population to run on the enterframe event - by that point you app has already finished loading. The init function could show an image saying “Loading…” so that user does not think your app has hung. [import]uid: 11393 topic_id: 4704 reply_id: 15589[/import]