0

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Geri Borbás
  • 15,810
  • 18
  • 109
  • 172
  • Does the answer to this question help: http://stackoverflow.com/q/12113961/558933 – Robotic Cat Mar 11 '14 at 00:16
  • 1
    http://www.cimgf.com/2014/02/25/deleting-objects-in-core-data/ is a good read on this topic. The faulting errors normally indicate references to deleted objects which are accessed after deleting. – Martin Brugger Mar 12 '14 at 05:27

2 Answers2

7

This should work for you

[ClassName MR_truncateAll];

[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];

ClassName here is the NSManagedObject subclass for your entity.

Amit
  • 836
  • 8
  • 19
-1

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];
Geri Borbás
  • 15,810
  • 18
  • 109
  • 172
  • 1
    It looks like you are not actually saving your deletes to the store, at least with this snippet. TruncateAll basically marks those objects for deletion on the next save. Try saving your deletes. – casademora Mar 11 '14 at 09:27
  • Yap, I switched reset to a save. It works, though it still shows faulting sometimes. I work with 30 entitites at all, though it shows faults on p100-p150 entities... Anyway, http://thermal-core.com/CoreDataEditor/ shows the right 30 entitiy. There is too many unexpected behaviour in Core Data, though, I understand it is designed for performance. – Geri Borbás Mar 11 '14 at 10:58