magicalpanda / MagicalRecord

Super Awesome Easy Fetching for Core Data!
Other
10.79k stars 1.79k forks source link

Why is the first record the only one to be added to the CD store? #854

Closed Spokane-Dude closed 10 years ago

Spokane-Dude commented 10 years ago

I have created a TAB-delimited file that I am using for backup purposes; the code below is used to restore the file. There are 5 records on the file; the first reccord contains the tab headings, the remainder the data.

When doing the restore, only the first record is restored, the other 3 are marked as unsuccessful. Here is the restore code:

- (IBAction)aRestoreCoreData:(UIButton *)sender {  //  user has to move file to iTunes so we can get it and restore store

NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

//  read the file back into databuffer...
NSFileHandle *readFile = [NSFileHandle fileHandleForReadingAtPath:[documentsPath stringByAppendingPathComponent: @"Backup.zip"]];
NSData *databuffer = [readFile readDataToEndOfFile];
[readFile closeFile];

// decompress the file
NSData *uncompressedData = [databuffer gunzippedData];

[Books MR_truncateAll];  //  truncate books entity

Books *bks = [Books MR_createEntity];  //  create entity

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

//  convert NSData to NSString (one big string!)  contains the header row!
NSString *inputString = [[NSString alloc] initWithData:uncompressedData encoding:NSUTF8StringEncoding];
NSArray *inputStringArray = [inputString componentsSeparatedByString:@"\n"];

//  put each row's columns in in Books elements
for(int i = 1; i < inputStringArray.count-1; i++)  {

NSArray *inputFields = [inputStringArray[i] componentsSeparatedByString:@"\t"];  //  contains data fields for each book

//  fill record fields from strings in array
bks.author = inputFields[0];
bks.binding = inputFields[1];
bks.bookCondition = inputFields[2];
bks.bookDescription = inputFields[3];
bks.bookNotes = inputFields[4];
bks.bookSize = inputFields[5];
bks.cost = [NSDecimalNumber decimalNumberWithString:inputFields[6]];
bks.dateAdded = [dateFormatter dateFromString:inputFields[7]];
bks.dateUpdated = [dateFormatter dateFromString:inputFields[8]];
bks.quarterUpdated =  inputFields[9];
bks.yearUpdated =  inputFields[10];
bks.doNotRepriceFlag = inputFields[11];
bks.edition =  inputFields[12];
bks.illustrator =  inputFields[13];
bks.imageFilename = inputFields[14];
bks.invoiceNbr = inputFields[15];
bks.isbn =  inputFields[16];
bks.jacket = inputFields[17];
bks.keywords = inputFields[18];
bks.shelf = inputFields[19];
bks.numberOfCopies = [NSNumber numberWithInt:[inputFields[20] intValue]];
bks.pages = [NSNumber numberWithInt:[inputFields[21] intValue]];
bks.price = [NSDecimalNumber decimalNumberWithString:inputFields[22]];
bks.primaryCatalog = inputFields[23];
bks.printing = inputFields[24];
bks.privateNotes = inputFields[25];
bks.pubLocation = inputFields[26];
bks.pubName = inputFields[27];
bks.pubYear = inputFields[28];
bks.quantity = [NSNumber numberWithInt:[inputFields[29] intValue]];
bks.secondaryCatalog = inputFields[30];
bks.shippingDomStd = (BOOL)inputFields[31];
bks.shippingDomExp = (BOOL)inputFields[32];
bks.shipping1DayDom = (BOOL)inputFields[33];
bks.shipping2DayDom = (BOOL)inputFields[34];
bks.shippingIntlStd = (BOOL)inputFields[35];
bks.shippingIntlExp = (BOOL)inputFields[36];
bks.signedBy = inputFields[37];
bks.sku = inputFields[38];
bks.status = inputFields[39];
bks.title = inputFields[40];
bks.tranCode = inputFields[41];
bks.volume = inputFields[42];
bks.weight = [NSNumber numberWithInt:[inputFields[43] intValue]];

[defaultContext MR_saveToPersistentStoreWithCompletion: ^(BOOL success, NSError *error) {
    if (error) {
        NSLog(@"-->  Error - SKU: %@",inputFields[38]);
    }
    else if(success) {
        NSLog(@"-->  Added - SKU: %@",inputFields[38]);
    }
    else
        NSLog(@"-->  Unsuccessful - SKU: %@",inputFields[38]);
}];
}
}

This is the output from the console:

2014-09-18 14:54:43.502 BookI[32513:60b] --> Added - SKU: 1 2014-09-18 14:54:43.502 Book[32513:60b] --> Unsuccessful - SKU: 2 2014-09-18 14:54:43.502 Book[32513:60b] --> Unsuccessful - SKU: 3 2014-09-18 14:54:43.502 Book[32513:60b] --> Unsuccessful - SKU: 4

What am I doing wrong? Sorry for the double post (here and SO) but I'm in a quandry on this one... Thanks...

Alydyn commented 10 years ago

Left an answer on SO: http://stackoverflow.com/a/25948551/1281846

tonyarnold commented 10 years ago

@Alydyn's answer on StackOverflow is correct. You're only creating one entity, and then looping over and changing its attributes. I'll let the conversation continue on SO where @Alydyn can collect some points for a good answer :smile:

Alydyn commented 10 years ago

Thanks @tonyarnold :smile: