我写了一个Mac App来生成我的sqlite文件,然后将其复制到我的iOS App。
当我想更改iOS App中的某些数据时,我发现无法保存更改。
错误是:Error Domain=NSCocoaErrorDomain Code=134030
在Mac上创建sqlite的代码:
NSManagedObjectModel *mode = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelPath];
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mode];
NSDictionary *options = @{
NSReadOnlyPersistentStoreOption : @YES,
NSSQLitePragmasOption: @{@"journal_mode":@"DELETE"}
};
[coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:sqlitePath options:options error:nil];
NSManagedObjectContext *managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
managedObjectContext.persistentStoreCoordinator = coordinator;
managedObjectContext.undoManager = nil;
[managedObjectContext performBlockAndWait:^{
for (ClipCard *currentCard in self.allCards)
{
ClipItem *clipInfo = [NSEntityDescription insertNewObjectForEntityForName:@"ClipItem" inManagedObjectContext:managedObjectContext];
clipInfo.clipName = currentCard.clipName;
}
[managedObjectContext save:nil];
[managedObjectContext reset];
}];
iOS上的读取和更改代码:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator == nil)
{
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];
NSURL *storeClipData = [[NSBundle mainBundle] URLForResource:@"ClipItems" withExtension:@"sqlite"];
NSDictionary *options = @{
NSReadOnlyPersistentStoreOption : @YES,
NSSQLitePragmasOption: @{@"journal_mode":@"DELETE"}
};
[_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeClipData options:options error:nil];
}
return _persistentStoreCoordinator;
}
//fetch data
NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"ClipItem"];
fetch.returnsObjectsAsFaults = false;
NSError *error = nil;
self.clipItems = [self.managedObjectContext executeFetchRequest:fetch error:&error];
//change some
if ([self.managedObjectContext hasChanges])
{
NSError *error = nil;
[self.managedObjectContext save:&error];
NSLog(@"save error: %@", error);
}
希望我已经明白了,任何帮助都会受到赞赏吗?
顺便说一句,我不知道为什么我的堆栈溢出帐户无法登录,所以我注册了一个新的。