私の iPad アプリの機能では、coredata データベース全体をときどきクリアする必要があります。私は次のようにこれを行います:
+(BOOL)clearEntireDB
{
// Empty all collections
NSArray* stores = [NSArray arrayWithObjects:@"ServerToken",@"Client",@"User",@"Form",@"Company",@"Contact",@"Response",@"Image",@"Thumbnail",@"SearchIndex",@"Tag",@"Literature", nil];
BOOL rc=YES;
for (int i=0;i<stores.count;i++)
{
if (rc==YES)
{
NSString* storeName = (NSString*)[stores objectAtIndex:i];
rc=[HPSDbUtilities clearStore:storeName];
}
}
if (rc==YES)
{
// Now need to delete all files in the document folder
rc = [HPSFileHelper removeAllDocumentFiles];
}
return rc;
}
+(BOOL)clearStore:(NSString*)storeName
{
BOOL rc=NO;
@try{
NSLog(@"HPSDbUtilities::clearStore %@",storeName);
HPSAppDelegate* appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext * context = appDelegate.managedObjectContext;
NSFetchRequest * fetch = [[NSFetchRequest alloc] init];
[fetch setEntity:[NSEntityDescription entityForName:storeName inManagedObjectContext:context]];
NSArray * result = [context executeFetchRequest:fetch error:nil];
for (id record in result)
[context deleteObject:record];
NSError* error;
if ([context save:&error] == YES)
{
NSLog(@"HPSDbUtilities::clearStore SAVED");
}else {
NSLog(@"HPSDbUtilities::clearStore NOT saved");
}
rc=YES;
}@catch (NSException * e) {
NSLog(@"clearStore Exception: %@", e);
rc=NO;
}
return rc;
}
アプリには、コア データ データベースと Web との同期を維持するセカンダリ スレッドがあります。データベース全体をクリアするときは、このセカンダリ スレッドを停止します。データベースがクリアされたら、セカンダリスレッドを再度開始します。
DB をクリアするまで、セカンダリ スレッド内のすべての処理は正常に機能します。セカンダリ スレッドの保存処理は、次のエラーで失敗し始めます。
Error Domain=NSCocoaErrorDomain Code=134030 "The operation couldn’t be completed. (Cocoa error 134030.)" UserInfo=0x261c10 {NSAffectedStoresErrorKey=(
"<NSSQLCore: 0xfe91310> (URL: file://localhost/var/mobile/Applications/3013185E-8171-4A1F-9E1F-6A02E7664510/Documents/Model.sqlite)"
), NSUnderlyingError=0x261b90 "The operation couldn’t be completed. (Cocoa error 4.)", NSFilePath=/var/mobile/Applications/3013185E-8171-4A1F-9E1F-6A02E7664510/Documents/Model.sqlite}
セカンダリ スレッド コードは複数のクラスに分散しているため、ここですべてを示すことはできませんが、簡単で単純に MOC を作成し、レコードを更新し、MOC を保存します。
誰でもこの問題に光を当てることができますか?