iOS: How to Populate CoreData with downloaded CSV

2019-04-01 18:06发布

I have been working on an app where the user inputs data stored in core data everyday (two attributes an NSNumber and one as NSDate) and I wanted to improve that by allowing the user to import data from a external file such as csv or any other supported format through a button click. Any suggestions on how to proceed efficiently to do this?

Thank you.

Edit: Just adding a screenshot of the csv file as well as the output of the csv parser as NSArray. Basicly need to fetch the attribute separately and store them in core data on button click.

- The input file as csv:

enter image description here

- Sample csv parser output(NSarray):

enter image description here

3条回答
聊天终结者
2楼-- · 2019-04-01 18:14

Try to use plist or json, they are already supported on iOS instead of CSV. CSV would require an third party parser. Using json or plist you will only need to loop throught the elemnts of the collections to create you persistent store. If you have just the CSV you can do a mid conversion using different free tools that you can find for free on the internet and later add to your bundle or publish to your site.

查看更多
姐就是有狂的资本
3楼-- · 2019-04-01 18:27

I needed to achieve something similar recently.

A couple of members of my project team wanted to take our app prototype out to show potential clients, but wanted to show different data to each client. We solved this by allowing members of our project team to create their own test data before meeting with the client.

I achieved this by creating an example .csv file and distributing it to the other guys in the project team. They populate it with their own test data and use iTunes File Sharing to drop the .csv test data file on to the device.

On load, the app scans its Documents directory for a the test data file. If it exists, it parses the .csv file and persists to the database.

For the CSV parsing, I used Dave DeLong's CHCSVParser: https://github.com/davedelong/CHCSVParser

Plenty of help is available on setting up iTunes file sharing for your app. A quick Google finds this tutorial (http://www.raywenderlich.com/1948/how-integrate-itunes-file-sharing-with-your-ios-app) which should help you out, if you need it.

Edit- added help on storing data from .csv in Core Data

You stated in your original post that you store an NSNumber and NSDate. Taking that as a starting point, you might have a .csv file in the following form:

+----------------+--------------+
+ NSNumberColumn | NSDateColumn |
+----------------+--------------+
+        1       |  2013-05-15  |
+        2       |  2013-06-15  |
+        3       |  2013-07-15  |
+----------------+--------------+

Assuming the output from the CSV parser is an NSArray of NSArrays, you could create the Core Data objects as follows:

I would create a couple of macros for the column numbers:

#define NSNumberColumn 0
#define NSDateColumn 1

Then iterate over the rows in the .csv file:

NSArray *rows = [NSArray arrayWithContentsOfCSVFile:pathToFile]; //CHCSVParser specific parsing method
for (NSArray *row in rows)
{
    NSString *numberString = [parsedCsvRow objectAtIndex:NSNumberColumn];
    NSString *dateString = [parsedCsvRow objectAtIndex:NSDateColumn];   

    NSNumber *number = // parse numberString to an NSNumber. Plenty of other posts on achieving this. 
    NSDate *date = // parse NSDate from dateString. Plenty of other posts on achieving this.

    NSManagedObjectContext *context = [self managedObjectContext];
    NSManagedObject *myCoreDataObject = [NSEntityDescription insertNewObjectForEntityForName:@"MyCoreDataObject" inManagedObjectContext:context];
    [myCoreDataObject setValue:number forKey:@"NSNumberColumn"];
    [myCoreDataObject setValue:date forKey:@"NSDateColumn"];
    NSError *error;
    if (![context save:&error]) {
        NSLog(@"%@", [error localizedDescription]);
    }
}

Note: Input validation and null checks have been ommited for brevity. I have also taken the liberty of making up your NSManagedObject property names, this will need updating. The above code should be separated in to a more suitable class structure.

I'm not at a Mac right now, so unfortunately I can't check if this works.

Hope that helps.

查看更多
家丑人穷心不美
4楼-- · 2019-04-01 18:27

Here's what you do when you already have your CSV file parsed and data is ready to use in Objective-C.

  1. Create a separate context for the import. You don't know how big the data can be, so you probably don't want to block one of your existing contexts while importing.

  2. Iterate through the entries in the parsed data and insert new managed objects configured from each entry.

  3. Every 200, 500, or 1000 entries (different for everybody, you'll need to test what's working best for you) save the context and, if needed, post a notification that a batch has been imported.

  4. To keep the memory low, reset the context and forget all the objects that you created in this import context.

  5. After the loop is finished, don't forget to save the last time.

Now how do you bring the data into another context, say, UI context?

This depends on the way you organized your Core Data stack. For example, import context can be configured as a child of the UI context. In this case, after each save to the import context the changes will be pushed to the UI context (and don't forget to save the UI context as well to push changes further).

But this is not the most efficient approach, because UI context, which is a context on the main thread, is involved in the import, and additional work is done on the UI thread that blocks it. I recommend creating the import context not as a child, but connected to the persistent store coordinator directly. To bring changes to the UI context in this case you either need to call mergeChangesFromContextDidSaveNotification: method after each save or you just refetch in the UI context after each save and in the end. The latter is easier on the UI context and particularly on NSFetchedResultsController, if you use it, because it doesn't need to replay changes to the updated objects one-by-one.

查看更多
登录 后发表回答