在iOS7中实现app图标徽章的后台获取时,核心数据表格被删除

时间:2014-08-15 19:11:33

标签: objective-c iphone core-data ios7

我正在尝试在AppDelegate.m中实现后台获取,以通过RestKit(此处未显示)从Web服务中提取最新记录数来获取应用程序图标徽章以反映更新的记录数。当我在Xcode中模拟BackGroundFetch时,一切正常。当我的iPhone 5S运行后台提取时,我的整个表" TicketList"在没有数据的情况下被彻底消灭这是代码。有没有人经历过类似的事情,任何人都可以提出一些关于我做错的建议吗?我已经取出了ReskKit查找来简化代码并进行调试,以确定它是否会产生任何影响。我查看了其他帖子,并且找不到为什么会发生这种情况的答案。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    //Added this code for the app icon badge functionality
    [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
    return YES;
}

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)   (UIBackgroundFetchResult))completionHandler
{
    [UIApplication sharedApplication].applicationIconBadgeNumber = 1;
    completionHandler(UIBackgroundFetchResultNewData);
}

*/
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    intAvailCount = 0;
    [self setUpCoreDataStack];
    NSManagedObjectContext *context = [self managedObjectContext];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"TicketList" inManagedObjectContext:context];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:entity];

    self.ticketListData = [[context executeFetchRequest:fetchRequest error:&error] mutableCopy];

    if ([ticketListData count] != 0){
        //Perform loop through TicketList SQLite DB and count using intAvailCount
        TicketList *info = [ticketListData objectAtIndex:0];

        for (info in ticketListData) {
            //Check to see if tickets in table have been added to do
            intAvailCount++;
        }
        //End Loop
    }
    [UIApplication sharedApplication].applicationIconBadgeNumber = intAvailCount;
    completionHandler(UIBackgroundFetchResultNewData);
}

-(void)setUpCoreDataStack
{
    NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:[NSBundle allBundles]];
    NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];

    NSURL *url = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:@"TicketList.sqlite"];

    NSDictionary *options = @{NSPersistentStoreFileProtectionKey: NSFileProtectionComplete,
                              NSMigratePersistentStoresAutomaticallyOption:@YES};

    NSError *error = nil;
    NSPersistentStore *store = [psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:options error:&error];
    if (!store)
    {
        NSLog(@"Error adding persistent store. Error %@",error);

        NSError *deleteError = nil;
        if ([[NSFileManager defaultManager] removeItemAtURL:url error:&deleteError])
        {
            error = nil;
            store = [psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:options error:&error];
        }

        if (!store)
        {
            // Also inform the user...
            NSLog(@"Failed to create persistent store. Error %@. Delete error %@",error,deleteError);
            abort();
        }
    }
    self.managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    self.managedObjectContext.persistentStoreCoordinator = psc;
}

1 个答案:

答案 0 :(得分:0)

问题解决了。我将调用setUpCoreDataStack转移到我的示例中的第一个方法(didFinishLaunchingWithOptions),它在应用启动时调用,我的问题解决了。不确定为什么会发生这种情况,但是,我认为这与我每次触发执行后台获取方法时调用的setUpCoreDataStack有关。