NSTimer导致错误(故障) - Objective-C

时间:2017-06-09 18:05:42

标签: ios objective-c nstimer

我有一个每60秒运行一次的计时器,但出于某种原因,在运行的前2次后,它会以随机的短间隔开始运行,直到程序崩溃。在4分钟内它运行7次然后崩溃。

- (void) resetDatabase {
    count++;

    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
        ConDAO *con = [[ConDAO alloc] init];
        DatabaseManager *manager = [DatabaseManager sharedManager];
        NSError * error;
        //        NSURL * storeURL = [[[manager managedObjectContext] persistentStoreCoordinator] URLForPersistentStore:[[[[manager managedObjectContext] persistentStoreCoordinator] persistentStores] lastObject]];
        //        [[manager managedObjectContext] reset];//to drop pending changes
        //        if ([[[manager managedObjectContext] persistentStoreCoordinator] removePersistentStore:[[[[manager managedObjectContext] persistentStoreCoordinator] persistentStores] lastObject] error:&error])
        //        {
        //            // remove the file containing the data
        //            [[NSFileManager defaultManager] removeItemAtURL:storeURL error:&error];
        //            //recreate the store like in the  appDelegate method
        //            [[[manager managedObjectContext] persistentStoreCoordinator] addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error];//recreates the persistent store
        //        }



        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Caf_student_cards"];
        NSArray *arr = [[manager managedObjectContext] executeFetchRequest:fetchRequest error:&error];
        if(error != nil){
            [self showError];
            return;
        }
        for (NSManagedObject *data in arr){
            [[manager managedObjectContext] deleteObject:data];
        }


        NSLog(@"*****************************");
        NSLog(@"updating");
        NSLog(@"count: %d", count);
        NSLog(@"*****************************");
        dispatch_async(dispatch_get_main_queue(), ^(void){
            [self populateDatabase:0 con:con];

            double delayInSeconds = 10.0;
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

                [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(resetDatabase) userInfo:nil repeats:YES];
            });
        });

    });
}

0 个答案:

没有答案