4

我想在我的核心数据应用程序中禁用日志模式。这是代码:

- (NSPersistentContainer *)persistentContainer {
// The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it.
@synchronized (self) {
    if (_persistentContainer == nil) {
        _persistentContainer = [[NSPersistentContainer alloc] initWithName:@"My_App"];

        NSURL *url = [[self applicationFilesDirectory] URLByAppendingPathComponent:@"My_App.sqlite"];
        NSPersistentStoreDescription *description=[[NSPersistentStoreDescription alloc]initWithURL:url];
        NSMutableDictionary *pragmaOptions=[NSMutableDictionary dictionary];
        [pragmaOptions setObject:@"DELETE" forKey:@"journal_mode"];
        [description setOption:pragmaOptions forKey:@"NSQlitePragmaOptions"];
        _persistentContainer.persistentStoreDescriptions=@[description];
        [_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
            if (error != nil) {
                // Replace this implementation with code to handle the error appropriately.
                // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

                /*
                 Typical reasons for an error here include:
                 * The parent directory does not exist, cannot be created, or disallows writing.
                 * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                 * The device is out of space.
                 * The store could not be migrated to the current model version.
                 Check the error message to determine what the actual problem was.
                */
                NSLog(@"Unresolved error %@, %@", error, error.userInfo);
                abort();
            }
        }];
    }
}

return _persistentContainer;}

我也验证了这个:

NSArray *descriptionArray=[_persistentContainer persistentStoreDescriptions];
NSPersistentStoreDescription * description=[descriptionArray objectAtIndex:0];
NSLog(@"%@",description.options);

NSLog 的结果是:

{
NSInferMappingModelAutomaticallyOption = 1;
NSMigratePersistentStoresAutomaticallyOption = 1;
NSQlitePragmaOptions =     {
    "journal_mode" = DELETE;
};

但是当我重新启动我的应用程序时,我总是在应用程序目录中找到所有三个文件 My_App.sqlite、My_App.sqlite-shm 和 My_App.sqlite-wal。

为什么它不起作用?

4

0 回答 0