iOS和Core Data中for循环内的奇怪多个日志输出

时间:2014-01-21 08:23:55

标签: ios objective-c core-data nslog

我在iOS 7应用程序和Xcode 5中使用带有Core Data复选框的空应用程序模板,当我编写以下代码以便在Core Data中进行后台提取时:

-(void)populateDatabase {
    for (NSUInteger counter = 0; counter < 1000; counter++) {
        Person *person = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([Person class]) inManagedObjectContext:self.managedObjectContext];
        person.firstName = [NSString stringWithFormat:@"First name %lu", (unsigned long)counter];
        person.lastName = [NSString stringWithFormat:@"Last name %lu", (unsigned long)counter];
        person.age = @(counter);
    }
    NSError *error = nil;
    if ([self.managedObjectContext save:&error]) {
        NSLog(@"managed to populate the database");
    }else{
        NSLog(@"failed to populate the database, error at %@", error);
    }
}


    -(NSFetchRequest *)newFetchRequest {
        NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([Person class])];
        request.fetchBatchSize = 20;
        request.predicate = [NSPredicate predicateWithFormat:@"(age >= 100) AND (age 

-(void)processPersons:(NSArray *)paramPersons {
    for (Person *person in paramPersons) {
        NSLog(@"First name = %@, last name = %@, age = %ld", person.firstName, person.lastName, (long)person.age.integerValue);
    }
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    [self populateDatabase];

    __weak NSManagedObjectContext *mainContext = self.managedObjectContext;
    __weak AppDelegate *weakSelf = self;
    __block NSMutableArray *mutablePersons = nil;

    NSManagedObjectContext *backgroundContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    backgroundContext.persistentStoreCoordinator = self.persistentStoreCoordinator;

    [backgroundContext performBlock:^{
        NSError *error = nil;
        NSArray *personIDs = [backgroundContext executeFetchRequest:[weakSelf newFetchRequest] error:&error];
        if (personIDs != nil && error == nil) {
            mutablePersons = [[NSMutableArray alloc] initWithCapacity:personIDs.count];
            dispatch_async(dispatch_get_main_queue(), ^{
                for (NSManagedObjectID *personId in personIDs) {
                    Person *person = (Person *)[mainContext objectWithID:personId];
                    [mutablePersons addObject:person];
                }
                [weakSelf processPersons:mutablePersons];
            });
        }else {
            NSLog(@"failed to execute the fetch request");
        }
    }];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

and then execute it, the debugging screen outputs about three to five times for each iteration of the loop in processPerson: method. Why does such strange behavior occur despite me writing the debug function only one time per each iteration.

-(void)processPersons:(NSArray *)paramPersons { for (Person *person in paramPersons) { NSLog(@"First name = %@, last name = %@, age = %ld", person.firstName, person.lastName, (long)person.age.integerValue); } } 的类型设置为字符串,年龄为整数32。

感谢。

1 个答案:

答案 0 :(得分:1)

因为您在每次启动时填充数据库并保存它。

如果您只是调试,请在运行一次后注释掉[self populateDatabase];