gpt4 book ai didi

ios - 核心数据 : Data is not been saved up

转载 作者:行者123 更新时间:2023-11-29 03:14:20 24 4
gpt4 key购买 nike

同时关注 http://timroadley.com/ 的电子书我正在插入。但是当我检查 sqlite 时,它​​们中没有数据。我也使用 -com.apple.CoreData.SQLDebug 进行调试,但没有显示查询。 Source Code

解决方案:- 只有当我终止应用程序或应用程序在按下主页按钮后进入后台时,数据才会显示在 sqlite 中。

AppDelegate.h

    #import <UIKit/UIKit.h>
#import "CoreDataHelper.h"
#import <CoreData/CoreData.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, strong, readonly) CoreDataHelper *coreDataHelper;
@end

AppDelegate.m

- (void)demo {

NSArray *newItemNames = [NSArray arrayWithObjects:
@"Apples", @"Milk", @"Bread", @"Cheese", @"Sausages", @"Butter", @"Orange Juice", @"Cereal", @"Coffee", @"Eggs", @"Tomatoes", @"Fish", nil];
for (NSString *newItemName in newItemNames) {
Item *newItem =
[NSEntityDescription insertNewObjectForEntityForName:@"Item" inManagedObjectContext:_coreDataHelper.context];
newItem.name = newItemName;
NSLog(@"Inserted New Managed Object for '%@'", newItem.name);
}
}

- (CoreDataHelper*)cdh {
if (!_coreDataHelper) {
_coreDataHelper = [CoreDataHelper new];
[_coreDataHelper setupCoreData];
}
return _coreDataHelper;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[self cdh] saveContext];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[self cdh];
[self demo];
}

- (void)applicationWillTerminate:(UIApplication *)application
{
[[self cdh] saveContext];
}

CoreDataHelper.h

@property(nonatomic,readonly) NSManagedObjectContext *context;
@property(nonatomic,readonly) NSManagedObjectModel *model;
@property(nonatomic,readonly) NSPersistentStore *store;
@property(nonatomic,readonly) NSPersistentStoreCoordinator *coordinator;

-(void)setupCoreData;
-(void)saveContext;

CoreDataHelper.m

#pragma mark - FILES
NSString *storeFilename = @"Grocery-Dude.sqlite";

#pragma mark - PATHS
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) lastObject];
}
- (NSURL *)applicationStoresDirectory {
NSURL *storesDirectory =
[[NSURL fileURLWithPath:[self applicationDocumentsDirectory]]
URLByAppendingPathComponent:@"Stores"];

NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:[storesDirectory path]]) {
NSError *error = nil;
if ([fileManager createDirectoryAtURL:storesDirectory
withIntermediateDirectories:YES
attributes:nil
error:&error]) {
}
else {
NSLog(@"FAILED to create Stores directory: %@", error);}
}
return storesDirectory;
}
- (NSURL *)storeURL {
return [[self applicationStoresDirectory]
URLByAppendingPathComponent:storeFilename];
}

#pragma mark - SETUP
- (id)init {
self = [super init];
if (!self) {return nil;}
_model = [NSManagedObjectModel mergedModelFromBundles:nil];
_coordinator = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:_model];
_context = [[NSManagedObjectContext alloc]
initWithConcurrencyType:NSMainQueueConcurrencyType];
[_context setPersistentStoreCoordinator:_coordinator];
return self;
}
- (void)loadStore {
if (_store) {return;} // Don’t load store if it's already loaded
NSDictionary *options =
@{NSSQLitePragmasOption: @{@"journal_mode": @"DELETE"}};

NSError *error = nil;
_store = [_coordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:[self storeURL]
options:options error:&error];
if (!_store) {NSLog(@"Failed to add store. Error: %@", error);abort();}
else {NSLog(@"Successfully added store: %@", _store);}
}
- (void)setupCoreData {
[self loadStore];
}

#pragma mark - SAVING
- (void)saveContext {

if ([_context hasChanges]) {
NSError *error = nil;
if ([_context save:&error]) {
NSLog(@"_context SAVED changes to persistent store");
} else {
NSLog(@"Failed to save _context: %@", error);
}
} else {
NSLog(@"SKIPPED _context save, there are no changes!");
}
}

最佳答案

尝试在循环本身中保存item,例如

NSArray *newItemNames = [NSArray arrayWithObjects:
@"Apples", @"Milk", @"Bread", @"Cheese", @"Sausages", @"Butter", @"Orange Juice", @"Cereal", @"Coffee", @"Eggs", @"Tomatoes", @"Fish", nil];
for (NSString *newItemName in newItemNames) {
Item *newItem =
[NSEntityDescription insertNewObjectForEntityForName:@"Item" inManagedObjectContext:_coreDataHelper.context];
newItem.name = newItemName;
NSLog(@"Inserted New Managed Object for '%@'", newItem.name);
[[self cdh] saveContext];
}

关于ios - 核心数据 : Data is not been saved up,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21852353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com