首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >保存在缓存/快照中的快照

保存在缓存/快照中的快照
EN

Stack Overflow用户
提问于 2012-10-07 17:29:35
回答 1查看 922关注 0票数 0

**Hi every1,我的应用程序的快照正在缓存/快照文件夹中记录。出于安全考虑,我不需要记录这些快照。我使用了下面这段代码,它是我从net获得的,用来删除保存的快照:

代码语言:javascript
复制
- (void)applicationDidEnterBackground:(UIApplication *)application {
UIApplication* app = [UIApplication sharedApplication];
    UIBackgroundTaskIdentifier __block bgTask;
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];

    if(UIBackgroundTaskInvalid != bgTask) {
        // Start the long-running task to kill app after some secs and return immediately.
        dispatch_after( dispatch_time(DISPATCH_TIME_NOW, KILL_IN_BACKGROUND_AFTER_SECS * 1e09), 
                       dispatch_get_main_queue(), ^{
                           if(goingToQuit) exit(0);
                           [app endBackgroundTask: bgTask];
                       });
    }
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // cancel ongoing background suicide.
    goingToQuit = NO;

}

我不想记录这些快照,请对此提出建议。**

EN

回答 1

Stack Overflow用户

发布于 2012-10-08 14:14:14

我认为您没有添加代码来删除文件:(请参考我的代码片段

代码语言:javascript
复制
- (void) deleteFiles {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self beingBackgroundUpdateTask];
        NSError *error = nil;
        // dirPath is path to your snapshot directory 
        NSArray *directoryContents = [fileMgr contentsOfDirectoryAtPath:dirPath error:&error];
        if (error == nil) {
            for (NSString *path in directoryContents) {
                 NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:path];
                 BOOL removeSuccess = [fileMgr removeItemAtPath:fullPath error:&error];
                 if (!removeSuccess) {
                     // Error handling
                     ...
                 }
            }
        } else {
            // Error handling
        }

        // Do something with the result 
       [self endBackgroundUpdateTask];
    });
}
- (void) beingBackgroundUpdateTask {
    self.backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [self endBackgroundUpdateTask];
    }];
}

- (void) endBackgroundUpdateTask {
    [[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask];
    self.backgroundUpdateTask = UIBackgroundTaskInvalid;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12767629

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档