質問

I use empty application template with Core Data checkbox being on in iOS 7 application and Xcode 5, and when I write the following code in order to do background fetch in 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.

I created Person entity on which firstName and lastName's type are set at string and age at integer32.

Thanks.

役に立ちましたか?

解決

Because you’re populating the database on each launch and saving it.

If you’re just debugging, comment out [self populateDatabase]; after it runs once.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top