1

I have the above CoreData scheme. How can i delete everything in it ? Is there an easy and quick way ?

enter image description here

Update:

Ok i try this

func flushDatabase() {
    if let moc = self.managedObjectContext{
        moc.lock()

        if let stores = persistentStoreCoordinator?.persistentStores as? [NSPersistentStore] {
            for store in stores {
                persistentStoreCoordinator?.removePersistentStore(store, error: nil)
                if let path = store.URL?.path {
                    NSFileManager.defaultManager().removeItemAtPath(path, error: nil)
                }
            }
        }

        moc.unlock()



    }
}

But how i recreate the persistenStoreCoordinator ?

Update 2

 class func deleteAllMinigames(context:NSManagedObjectContext) {
        context.lock()
        let fetchRequest = NSFetchRequest(entityName: "Minigame")
//        fetchRequest.includesSubentities = true
//        fetchRequest.includesPropertyValues = false
        var error: NSError?
        if let items = context.executeFetchRequest(fetchRequest, error: &error) as? [Minigame]{
            for item in items {
                if item.validateForDelete(&error){
                    context.deleteObject(item)
                }else{
                    println(error?.localizedDescription)
                }

            }
        }
        context.unlock()

    }

When i trying to delete all minigames i get this. I tried to make all delete to rules to cacsade but its not working

*Optional("The operation couldn’t be completed. (Cocoa error 1560.)")
Optional("The operation couldn’t be completed. (Cocoa error 1560.)")
Optional("The operation couldn’t be completed. (Cocoa error 1560.)")
Optional("The operation couldn’t be completed. (Cocoa error 1560.)")
Optional("The operation couldn’t be completed. (Cocoa error 1560.)")
Optional("The operation couldn’t be completed. (Cocoa error 1560.)")*
  • The "Related" section on the right shows some similar questions (with answers). Which one did you try? – Martin R Apr 24 '15 at 07:21
  • 3
    possible duplicate of [Delete/Reset all entries in Core Data?](http://stackoverflow.com/questions/1077810/delete-reset-all-entries-in-core-data) – Mrunal Apr 24 '15 at 07:22
  • Have you tried ctrl+click in all the tables and press `return`? – Kutyel Apr 24 '15 at 07:32

3 Answers3

2

It means there's a mandatory property has been assigned nil. Either in your *.xcodatamodel check the "optional" box or when you are saving to the managedObjectContext make sure that your properties are filled in.

If you're getting further errors after changing your code to suit the two requirements try cleaning your build and delete the application from your iPhone Simulator/iPhone device. Your model change may conflict with the old model implementation.

Edit:

I almost forgot here's all the error codes that Core Data spits out: Core Data Constants Reference I had trouble with this before and I realised I unchecked the correct optional box. Such trouble finding out the problem. Good luck.

bLacK hoLE
  • 781
  • 1
  • 7
  • 20
1

I suggest to use NSFileManager to remove the persistent data store file and setup the core data stack again.

Piotr Tobolski
  • 1,326
  • 7
  • 22
1

Since every entity is somehow connected to your Minigame, you can set the delete rules to cascade from there on the relationships, and just simply delete all your Minigames. It would automatically delete the rest, using somethinf like this:

+ (void)deleteAllMinigamesInManagedObjectContext:(NSManagedObjectContext *)context
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Minigame"];
    NSArray *fetchedObjects = [context executeFetchRequest:request error:NULL];
    for (Minigame *game in fetchedObjects) {
        [context deleteObject:game];
    }
    [context save:NULL];
}

Edit:

Between Multiplechoice and Answer set the rule only to nullify, because you have a loop there which might fail otherwise. How did you set the delete rules for the rest? The cascade only has to be unidirectional, so for example in Minigame for the relationship called coupon you need cascade, but for the inverse relationship (minigame in Coupon) you only need nullify

Levi
  • 7,313
  • 2
  • 32
  • 44