可以进行更新,但总是包含0行 - 核心数据更新

时间:2014-02-27 20:50:19

标签: ios objective-c core-data

为什么它总是在db中生成0行。如果有新记录然后是新条目,但当条目已经存在时,它应该更新,但只更新它找到的条目。但我也总是有0个条目,更新正在运行。我是否会错过理解foor循环?我认为它应该是那样的。

for ( int i=0;i<[rowCustomer count];i++)
{
    // NSLog(@"row customer %@",rowProductItem);
    if ([[rowCustomer objectAtIndex:i] isEqualToString:@""])  {
        continue;
    }

    componentsCustomer= [[rowCustomer objectAtIndex:i] componentsSeparatedByString:@";"];

    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
    int f = [[componentsCustomer objectAtIndex:0] intValue];
    int f1 = [[componentsCustomer objectAtIndex:18] intValue];
    int f2 = [[componentsCustomer objectAtIndex:3] intValue];            

    NSNumber *num1 = [NSNumber numberWithInt:f];
    NSNumber *num2 = [NSNumber numberWithInt:f1];
    NSNumber *num3 = [NSNumber numberWithInt:f2];            

    NSDateFormatter *dateFormater1 = [[NSDateFormatter alloc] init];
    [dateFormater1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateFromString1 = [dateFormater1 dateFromString:[componentsCustomer objectAtIndex:1]];
    NSDate *dateFromString2 = [dateFormater1 dateFromString:[componentsCustomer objectAtIndex:2]];

    NSDateFormatter *dateFormater2 = [[NSDateFormatter alloc] init];
    [dateFormater2 setDateFormat:@"yyyy-MM-dd"];
    NSDate *dateFromString3 = [dateFormater1 dateFromString:[componentsCustomer objectAtIndex:7]];

    Customer *newCustomer = [NSEntityDescription insertNewObjectForEntityForName:@"Customer" inManagedObjectContext:[self managedObjectContext]];

    NSFetchRequest *fetchcustomer = [NSFetchRequest fetchRequestWithEntityName:@"Customer"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(remoteID == %@) AND ( lastedited < %@ )",num1,dateFromString2];
    [fetchcustomer setPredicate:predicate];
    responseCustomer = [[[self managedObjectContext] executeFetchRequest:fetchcustomer error:&error] mutableCopy];

    NSFetchRequest *fetchcustomer2 = [NSFetchRequest fetchRequestWithEntityName:@"Customer"];
    NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"(remoteID == %@) AND (lastedited == %@)",num1,dateFromString2];
    [fetchcustomer2 setPredicate:predicate2];
    responseCustomer2 = [[[self managedObjectContext] executeFetchRequest:fetchcustomer2 error:&error] mutableCopy];

    if (responseCustomer != nil && [responseCustomer count] > 0) {

        if ([num2 intValue] > 1 ) {

            [self.managedObjectContext deleteObject:[responseCustomer firstObject]];
            [self saveContext];

        } else {

            anzahlCustomer = YES;

            // Now we have the object that we're looking for, we can update it

            [[responseCustomer firstObject] setValue:num1 forKey:@"remoteID"];
            [[responseCustomer firstObject] setValue:dateFromString1 forKey:@"created"];
            [[responseCustomer firstObject] setValue:dateFromString2 forKey:@"lastedited"];
            [[responseCustomer firstObject] setValue:num3 forKey:@"aktiv"];
            [[responseCustomer firstObject] setValue:[componentsCustomer objectAtIndex:4] forKey:@"hofname"];
            [[responseCustomer firstObject] setValue:[componentsCustomer objectAtIndex:5] forKey:@"kundenland"];
            [[responseCustomer firstObject] setValue:[componentsCustomer objectAtIndex:6] forKey:@"anrede"];
            [[responseCustomer firstObject] setValue:dateFromString3 forKey:@"birthday"];
            [[responseCustomer firstObject] setValue:[componentsCustomer objectAtIndex:8] forKey:@"cstreet"];
            [[responseCustomer firstObject] setValue:[componentsCustomer objectAtIndex:9] forKey:@"plz"];
            [[responseCustomer firstObject] setValue:[componentsCustomer objectAtIndex:10] forKey:@"place"];
            [[responseCustomer firstObject] setValue:[componentsCustomer objectAtIndex:11] forKey:@"latitude"];
            [[responseCustomer firstObject] setValue:[componentsCustomer objectAtIndex:12] forKey:@"longitude"];
            [[responseCustomer firstObject] setValue:[componentsCustomer objectAtIndex:13] forKey:@"phone"];
            [[responseCustomer firstObject] setValue:[componentsCustomer objectAtIndex:14] forKey:@"homepage"];
            [[responseCustomer firstObject] setValue:[componentsCustomer objectAtIndex:15] forKey:@"firstname"];
            [[responseCustomer firstObject] setValue:[componentsCustomer objectAtIndex:16] forKey:@"surname"];
            [[responseCustomer firstObject] setValue:[componentsCustomer objectAtIndex:17] forKey:@"email"];
            [[responseCustomer firstObject] setValue:num2 forKey:@"sync"];
            [[responseCustomer firstObject] setValue:@"Leerfeld" forKey:@"leerfeld"];
            [self saveContext];
        }            

    } else if(responseCustomer2 != nil && [responseCustomer2 count] < 1) {

        anzahlCustomer = YES;

        newCustomer.remoteID = num1;
        newCustomer.created = dateFromString1;
        newCustomer.lastedited = dateFromString2;
        newCustomer.aktiv = num3;
        newCustomer.hofname = [componentsCustomer objectAtIndex:4];
        newCustomer.kundenland = [componentsCustomer objectAtIndex:5];
        newCustomer.anrede = [componentsCustomer objectAtIndex:6];
        newCustomer.birthday = dateFromString3;
        newCustomer.cstreet = [componentsCustomer objectAtIndex:8];
        newCustomer.plz = [componentsCustomer objectAtIndex:9];
        newCustomer.place = [componentsCustomer objectAtIndex:10];
        newCustomer.latitude = [componentsCustomer objectAtIndex:11];
        newCustomer.longitude = [componentsCustomer objectAtIndex:12];
        newCustomer.phone = [componentsCustomer objectAtIndex:13];
        newCustomer.homepage = [componentsCustomer objectAtIndex:14];
        newCustomer.firstname = [componentsCustomer objectAtIndex:15];
        newCustomer.surname = [componentsCustomer objectAtIndex:16];
        newCustomer.email = [componentsCustomer objectAtIndex:17];
        newCustomer.sync = num2;
        newCustomer.leerfeld = @"leerfeld";

        [self saveContext];

    }
} // enf for loop

1 个答案:

答案 0 :(得分:0)

我必须从for循环中取出更新提取。就像现在一样,它总是创建所有循环并创建0条记录,即使它只是一条更新记录。 所以我将单独进行更新的获取问题。