ipatch
ipatch

Reputation: 4032

How to populate Core Data / SQLite DB with contents of NSMutableArray

I have an NSMutableArray named usersArray that I would like to populate a Core Data / SQLite DB / store. So far I have the following method constructed, but I don't think it will populate correctly, any help would be greatly appreciated.

- (void)populateCDDB:(NSMutableArray *)usersArray {

    NSData *arrayData = [NSKeyedArchiver archivedDataWithRootObject:usersArray];

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:[NSEntityDescription entityForName:@"Account" inManagedObjectContext:_context]];

    NSError *error = nil;
    NSArray *objectsForImport = [_context executeFetchRequest:request error:&error];
    NSArray *importKeys = [NSArray arrayWithObjects:@"username", @"pin", @"credit", @"email", @"lastLogin", @"rfid", @"phoneNumber", nil];

    for (NSManagedObject *object in objectsForImport) {
//        NSMutableArray *anObjectArray = [NSMutableArray arrayWithCapacity:[importKeys count]];
        for (NSString *key in importKeys) {
            id value = [object valueForKey:key];
            if (!value) {
                value = @"";
            }
            [usersArray addObject:[value description]];
        }
    }
    //save the results to Core Data / SQLite store.
    [_context save:&error];
}

And yes usersArray is an instance variable for the class.

Upvotes: 0

Views: 66

Answers (1)

Jody Hagins
Jody Hagins

Reputation: 28349

OK, I understand the comment to mean that you want to use the objects in the array as a source for creating "Account" objects in core data.

From the data gathered from the question, I assume that both your objects in the array and the core data "Account" entity support all of the keys.

Since you didn't specify a type or API for the objects in the array, I assume they are key-value compliant on the names you listed earlier, and the types are compatible.

- (BOOL)addUsersFromArray:(NSArray*)usersToAdd
                  intoMOC:(NSManagedObjectContext*)moc
                    error:(NSError**)error {
    NSArray *importKeys = @[@"pin", @"credit", @"email", @"lastLogin", @"rfid", @"phoneNumber"];
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Account"];
    for (id user in usersToAdd) {
        id username = [user valueForKey:@"username"];
        // See if we already have an Account for this user - username is unique key
        fetchRequest.predicate = [NSPredicate predicateWithFormat:@"username = %@", username];
        NSArray *fetched = [moc executeFetchRequest:fetchRequest error:error];
        if (fetched == nil) return NO;

        NSManagedObject *account = [fetched firstObject];
        if (account == nil) {
            account = [NSEntityDescription insertNewObjectForEntityForName:@"Account" inManagedObjectContext:moc];
            [account setValue:username forKey:@"username"];
        }
        for (NSString *key in importKeys) {
            id value = [user valueForKey:key];
            if (value) {
                [account setValue:value forKey:key];
            }
        }

    }
    return YES;
}

Upvotes: 1

Related Questions