为什么第一个记录是唯一一个添加到CD存储的记录?

时间:2014-09-18 22:02:49

标签: magicalrecord-2.2

我创建了一个用于备份目的的TAB分隔文件;下面的代码用于恢复文件。文件中有5条记录;第一个记录包含制表符标题,其余部分包含数据。

执行恢复时,仅恢复第一条记录,其他3条记录为不成功。这是恢复代码:

- (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]);
    }];
}
}

这是控制台的输出:

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

我做错了什么?

1 个答案:

答案 0 :(得分:2)

斯波坎-哥们, 我想你想在你的for循环中创建一个新实体,而不是重新使用同一个实体并覆盖属性和保存。

保存第一本图书实体后,您无法为后续记录创建任何其他新实体。