-1

I try to delete an object from coredata which matches with Test2. I tried this:

    let context = self.fetchedResultsController.managedObjectContext
    let fetchRequest = NSFetchRequest(entityName:"Person")
    fetchRequest.predicate = NSPredicate(format: "name = Test2")
    var error : NSError?
    let results = managedObjectContext!.executeFetchRequest(fetchRequest, error:&error)

    context.deleteObject(results.firstObject as NSManagedObject)

I get an error in this line of code: context.deleteObject(results.firstObject as NSManagedObject). It says: Cannot invoke 'deleteObject' with an argument list of type '(NSManagedObject)'. Does someone knows how I can solve this error?

UPDATE:

2015-07-12 20:43:09.601 Note[4222:138125] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to generate SQL for predicate (name == Test2) (problem on RHS)'
*** First throw call stack:
(
0   CoreFoundation                      0x000000010ebd6c65 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x0000000110741bb7 objc_exception_throw + 45
2   CoreData                            0x000000010e7f0bbc -[NSSQLGenerator newSQLStatementForRequest:ignoreInheritance:countOnly:nestingLevel:] + 1724
3   CoreData                            0x000000010e7dcdc4 -[NSSQLAdapter _statementForFetchRequest:ignoreInheritance:countOnly:nestingLevel:] + 244
4   CoreData                            0x000000010e6f4e0c -[NSSQLAdapter _newSelectStatementWithFetchRequest:ignoreInheritance:] + 316
5   CoreData                            0x000000010e6f4a86 -[NSSQLCore newRowsForFetchPlan:] + 118
6   CoreData                            0x000000010e6f433c -[NSSQLCore objectsForFetchRequest:inContext:] + 524
7   CoreData                            0x000000010e6f3dbb -[NSSQLCore executeRequest:withContext:error:] + 299
8   CoreData                            0x000000010e7cea6c __65-[NSPersistentStoreCoordinator executeRequest:withContext:error:]_block_invoke + 3356
9   CoreData                            0x000000010e7d7c30 gutsOfBlockToNSPersistentStoreCoordinatorPerform + 192
10  libdispatch.dylib                   0x0000000111e65614 _dispatch_client_callout + 8
11  libdispatch.dylib                   0x0000000111e4b002 _dispatch_barrier_sync_f_invoke + 365
12  CoreData                            0x000000010e7c9245 _perform + 197
13  CoreData                            0x000000010e6f3a58 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 504
14  CoreData                            0x000000010e6f22ca -[NSManagedObjectContext executeFetchRequest:error:] + 586
15  Note                               0x000000010e4efb72 _TFC5Note14ViewController19deleteSelectedTasksfS0_FT_T_ + 738
16  Note                               0x000000010e4ef843 _TFC5Note14ViewController7delTaskfS0_FT_T_ + 51
17  Note                               0x000000010e4ef882 _TToFC5Note14ViewController7delTaskfS0_FT_T_ + 34
18  Foundation                          0x000000010f011744 __NSFireTimer + 83
19  CoreFoundation                      0x000000010eb3e174 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
20  CoreFoundation                      0x000000010eb3dd35 __CFRunLoopDoTimer + 1045
21  CoreFoundation                      0x000000010eaffd3d __CFRunLoopRun + 1901
22  CoreFoundation                      0x000000010eaff366 CFRunLoopRunSpecific + 470
23  GraphicsServices                    0x000000011128ca3e GSEventRunModal + 161
24  UIKit                               0x000000010f475900 UIApplicationMain + 1282
25  Note                                0x000000010e5146c7 main + 135
26  libdyld.dylib                       0x0000000111e99145 start + 1
27  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
paro
  • 217
  • 3
  • 10

2 Answers2

0

You could use context.deleteObject(results?.first as! NSManagedObject) and it will compile but it will crash if it's not a NSManagedObject.

This is obviously not the right approach. Remember: ! are code smells, because in most of the cases you don't need them.

firstObject is a NSArray method, to use it you should specify that results is of type NSArray (i.e. let results: NSArray). The Swift standard library equivalent is first, and returns an optional value while deleteObject doesn't accept an optional. So you should unwrap it and you can check if it is of type NSManagedObject at the same time.

What about something like:

   if let results = context.executeFetchRequest(fetchRequest, error:&error),
      let managedObject = results.first as? NSManagedObject {
         context.deleteObject(managedObject)
   }
Luca Torella
  • 7,974
  • 4
  • 38
  • 48
  • I tried out your proposal. Now I the app crashes in this line of code: `if let results = context.executeFetchRequest(fetchRequest, error:&error),` with the following error: `Thread 1: breakpoint 1.2 3.2`. Here are a few (hopefully) useful images: https://www.dropbox.com/s/0ubnlnufodnsb2l/Bildschirmfoto%202015-07-12%20um%2019.27.06.png?dl=0 and https://www.dropbox.com/s/5m9vdx2tlgns59o/Bildschirmfoto%202015-07-12%20um%2019.28.18.png?dl=0. Hope it helps to solve the error. – paro Jul 12 '15 at 17:31
  • It seems like you have set some breakpoints in your code rather than an error. Please disable your breakpoints and it should work. – agy Jul 12 '15 at 18:39
  • I set an exception breakpoint. I disabled it but the app is still crashing. Now I also get an log entry (see my updated answer). Thanks for your help. – paro Jul 12 '15 at 18:44
  • @agy Do you know what I have to do? – paro Jul 13 '15 at 18:43
  • @paro find my answer posted – agy Jul 13 '15 at 19:09
0

The problem seems to be in your predicate definition. Please try:

fetchRequest.predicate = NSPredicate(format: "name = 'Test2'")

or

fetchRequest.predicate = NSPredicate(format: "%K = %@", "name", "Test2")

If you want to add a variable.

fetchRequest.predicate = NSPredicate(format: "%K = %@", "name", variable)
agy
  • 2,804
  • 2
  • 15
  • 22
  • Yes the first one finally worked. Thanks a lot for your help! – paro Jul 14 '15 at 18:01
  • I marked it as correct. Could you please tell me what I have to write if I wanna replace `Test2` for a string-variable? `(format: "name = '\(variable)'")` doesn't work. – paro Jul 15 '15 at 18:39
  • Please find my answer edited. I also recommend you this post http://nshipster.com/nspredicate/ – agy Jul 15 '15 at 18:47