Bob Green
Bob Green

Reputation: 19

How to load a table downloaded from the web into core data in iOS?

How do I load a table downloaded from the web and put it into core-data? If I have a CSV file (could be SQLite) of about 2000 rows, that's been updated, and put onto the web like Amazon Web Service, how can that be loaded into core-data while the user is using the app? I would like to completely replace the previous file, and reload the table. I imagine I save it to the documents directory, but then how to load it into core-data and it load quickly so the user does not have to wait too long?

I won't know what data the user needs, what was added or deleted since the last time it was used, so I can't make a query for just the changes. The table is made up of text, integers, and geocodes, that I will update twice a month.

Can core-data read from an external file to populate a tableView, or must it read only from what's inside itself? I don't absolutely need a relationship between tables, so if that gets in the way, I could remove it. There may or may not be a second table loaded. Would it be better to use SQLite table instead? I've not done this part of core data before. What are best practices? Code samples would be helpful.

Upvotes: 0

Views: 191

Answers (1)

Joride
Joride

Reputation: 3763

I assume you are well versed in using Core Data and you are deploying to iOS 5 and higher. I also assume you understand the CSV format very well. Then this would be a design to start out with:

  1. Create a parser object. This object is responsible for understanding the csv-file (this is logic that you need to write yourself, there is no native csv-parser clas). You give this object the NSString, or local URL to the file and read it line by line. Let's say every row contains the data for one Core Data entity: this parser then extracts the values from the string's first line and creates/updates an existing Core Data entity. See this link for an efficient way of updating/creating Core Data entities. Make sure all this parsing is done on a backgroundThread, with an NSManagedObjectContext that has a private queue type.
  2. Create a UITableViewController that utilizes an NSFetchedResultsController that you initialize with an NSManagedObjectContext that is the parent of the NSManagedObejctContext used in the parser-object. Make sure you give it the main queue concurrencytype on initialization. This way, every time the parser creates/updates an entity, it will save this up to the parent context. This context in turn notifies the NSFetchedResutsController and you can update your UI for each updated/created entity separately through the NSFetchedResultsController delegate methods, showing changes to the user as fast as you can.
  3. Make sure some overseeing object (might be a Data-manager, or maybe a networking class, or maybe event the viewController, although I would go for a Data-manager) gets told that the complete parsing process is over, so that the managedObject in the UITableViewController can be saved (which will actually persist the data).

Upvotes: 2

Related Questions