How to flush damn Core Data objects?

Excuse the wording please, I just want to delete objects for 20 minutes now. Deleting just fine, but Core Data raises exception after a while.

I tried so far:

Delete all objects (MR_truncateAll), then save.

Delete all objects, process pending changes, save.

Turn off undo manager, delete, turn back undo manager.

Delete Core Data sqlite file, destroy context, then recreate.

Use [MagicalRecord cleanUp], delete core data sqlite file.


Still, when I want to create object, after 2-3 attempt, I'll get Faulting, then crash because faulting.

CoreData could not fulfill a fault for '0xd000000000840002 <x-coredata://...

(
    0   CoreFoundation                      0x0000000102016795 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000101d79991 objc_exception_throw + 43
    2   CoreData                            0x0000000100265a93 _PFFaultHandlerLookupRow + 1075
    3   CoreData                            0x00000001002f33a3 -[NSManagedObject(_NSInternalMethods) _updateFromRefreshSnapshot:includingTransients:] + 243
    4   CoreData                            0x0000000100297563 -[NSManagedObjectContext(_NestedContextSupport) _copyChildObject:toParentObject:fromChildContext:] + 771
    5   CoreData                            0x000000010029701b -[NSManagedObjectContext(_NestedContextSupport) _parentProcessSaveRequest:inContext:error:] + 1019
    6   CoreData                            0x00000001002fd243 __82-[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:]_block_invoke + 563
    7   libdispatch.dylib                   0x0000000102ce005a _dispatch_barrier_sync_f_slow_invoke + 45
    8   libdispatch.dylib                   0x0000000102cef6fd _dispatch_client_callout + 8
    9   libdispatch.dylib                   0x0000000102cdf46c _dispatch_main_queue_callback_4CF + 354
    10  CoreFoundation                      0x0000000102074729 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    11  CoreFoundation                      0x0000000101fc19a4 __CFRunLoopRun + 1764
    12  CoreFoundation                      0x0000000101fc0ed3 CFRunLoopRunSpecific + 467
    13  GraphicsServices                    0x00000001032f03a4 GSEventRunModal + 161
    14  UIKit                               0x0000000100e48a63 UIApplicationMain + 1010
    15  GitHub!                             0x0000000100002f53 main + 115
    16  libdyld.dylib                       0x0000000102f9c7e1 start + 0
)

I use a single context. Created by MagicalRecord under the hood.

How do you delete, flush Core Data?

有帮助吗?

解决方案

This should work for you

[ClassName MR_truncateAll];

[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];

ClassName here is the NSManagedObject subclass for your entity.

其他提示

This works, though I'm not sure if entities really gets deleted (but I hope).

Still shows failing errors on every 2-3th try, but does not throw errors. I just cannt belive that they could not wrap up a [NSPersistentStoreCoordinator deleteEveryStuffSeriouslyNotKidding] method.

// Cut every retaining references.
self.someEntity = nil;
self.entityUsingRelations = nil;

[self.context processPendingChanges];
[[self.context undoManager] disableUndoRegistration];

// You can use simple Core Data `delete` methods.
[SomeEntity MR_truncateAll];
[ReleatiedEntity MR_truncateAll];
[AnotherReleatedEntity MR_truncateAll];
[EntityUsingRelations MR_truncateAll];

[self.context processPendingChanges];
[[self.context undoManager] enableUndoRegistration];

[self.context MR_saveToPersistentStoreAndWait];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top