Importing data with core-data

  • I am writing an application that needs to download a couple of tables
    (with a
    master- detail relationship)  and then merge them in to an existing
    core data sqlite
    store.  The download could be formatted as xml or CSV. Is it possible
    to do this
    using the coredata.dtd or would it make more sense to use a more
    generic xml
    tree structure to represent the master-detail relationship? Any
    thoughts or suggestions
    would be much appreciated.
  • Shaun,

    On Nov 4, 2006, at 7:12 AM, shaun bear wrote:

    > I am writing an application that needs to download a couple of
    > tables (with a
    > master- detail relationship)  and then merge them in to an existing
    > core data sqlite
    > store.  The download could be formatted as xml or CSV. Is it
    > possible to do this
    > using the coredata.dtd or would it make more sense to use a more
    > generic xml
    > tree structure to represent the master-detail relationship? Any
    > thoughts or suggestions
    > would be much appreciated.

    A database table is basically an array of dictionary objects. As long
    as all the fields are valid property list types, you can loop through
    the array creating a managed object for each dictionary and setting
    the values. If you have an array controller in your nib and it's
    configured for your managed object, you can use this (where plist is
    expected to be an NSArray* - not sure why I made it an id).

    - (void)addItemsFromPlist:(id)plist toController:(NSArrayController *)
    controller;
    {
    NSEnumerator *oe = [plist objectEnumerator];
    NSDictionary *dictionaryItem = nil;
    while(dictionaryItem = [oe nextObject]) {
      NSManagedObject *mo = [[controller newObject] autorelease];
      [mo setValuesForKeysWithDictionary:dictionaryItem];
      [controller addObject:mo];
    }
    }

    This is really fast even on my first generation MacBook Pro ;)

    Hope this helps
    Marc
previous month november 2006 next month
MTWTFSS
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      
Go to today
MindNode
MindNode offered a free license !