horin horin -3 years ago 123
Swift Question

Changing Core Data when app starts

I am developing an iOS app which downloads xml data that are valid for 4 hours. I want to check validity and, if needed, update this data when my app starts.

I am using this xml parser to load data http://www.theappguruz.com/blog/xml-parsing-using-nsxmlparse-swift

Right now I am calling

and parsing data in
function didiFinishLaunchingWithOptions
. Is this the correct place to perform this background task?

In tutorial which I posted the guy does it in view controller but I want to use this parser to update coredata and I need it to run in background after app launches.

Thanks in advance

Answer Source

The professional way to do this would be have a function (or even a full class), to manage this download/parse data and asynchronously save in your core data, after that, you can inform the view that you have new content to load (or if there was an error or something).

About where to call the function, it depends...

If you should only show to the user the most updated information (like locking the screen whit a "loading..." or something like it), put the call in the first view controller, just because would be simpler to just call an completion handler to unlock and load the data.

But, if you can load the "old" information, just to be faster and refresh when the new content is available, i think that you can call in the appDelegate with no problem.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download