核心数据,NSPredicate导致NSInvalidArgumentException

时间:2016-10-12 05:18:32

标签: core-data nspredicate

我在后台线程上调用此代码。

backgroundContext.performAndWait {
let fetchRequest:NSFetchRequest<BlogPost> = BlogPost.fetchRequest()
fetchRequest.predicate = NSPredicate.init(format: "postid=%@", argumentArray: [someInt64Value])  // this line creates an exception

_ = try? fetchRequest.execute()
....
}

如果我删除谓词,它可以正常工作。 如果在fetchResultController中使用,则相同的谓词可以正常工作。 postid是Int64,但如果我将谓词更改为任何其他键,它将给出相同的 NSInvalidArgumentException

这与Core Data Error - NSDate localizedCaseInsensitiveCompare: unrecognised selector sent to instance

非常相似
2016-10-11 21:58:13.918 BlogClient[44494:13192129] -[_SwiftValue longLongValue]: unrecognized selector sent to instance 0x618000243cc0
2016-10-11 21:58:13.922 BlogClient[44494:13192129] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue longLongValue]: unrecognized selector sent to instance 0x618000243cc0'
*** First throw call stack:
(
0   CoreFoundation                      0x000000010f59534b __exceptionPreprocess + 171
1   libobjc.A.dylib                     0x000000010ebd921e objc_exception_throw + 48
2   CoreFoundation                      0x000000010f604f34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3   CoreFoundation                      0x000000010f51ac15 ___forwarding___ + 1013
4   CoreFoundation                      0x000000010f51a798 _CF_forwarding_prep_0 + 120
5   CoreData                            0x000000010f09762e -[NSSQLiteConnection execute] + 2270
6   CoreData                            0x000000010f0aeaba newFetchedRowsForFetchPlan_MT + 1658
7   CoreData                            0x000000010f274467 _executeFetchRequest + 55
8   CoreData                            0x000000010f1f3be5 -[NSSQLFetchRequestContext executeRequestUsingConnection:] + 53
9   CoreData                            0x000000010f220eb7 __52-[NSSQLDefaultConnectionManager handleStoreRequest:]_block_invoke + 215
10  libdispatch.dylib                   0x0000000112b8b0cd _dispatch_client_callout + 8
11  libdispatch.dylib                   0x0000000112b6830a _dispatch_barrier_sync_f_invoke + 340
12  CoreData                            0x000000010f220d8c -[NSSQLDefaultConnectionManager handleStoreRequest:] + 236
13  CoreData                            0x000000010f22861f -[NSSQLCoreDispatchManager routeStoreRequest:] + 351
14  CoreData                            0x000000010f1ab7d9 -[NSSQLCore dispatchRequest:withRetries:] + 233
15  CoreData                            0x000000010f1a711d -[NSSQLCore processFetchRequest:inContext:] + 93
16  CoreData                            0x000000010f09dcf8 -[NSSQLCore executeRequest:withContext:error:] + 568
17  CoreData                            0x000000010f191b79 __65-[NSPersistentStoreCoordinator executeRequest:withContext:error:]_block_invoke + 5609
18  CoreData                            0x000000010f189966 -[NSPersistentStoreCoordinator _routeHeavyweightBlock:] + 390
19  CoreData                            0x000000010f09d87e -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 654
20  CoreData                            0x000000010f09be24 -[NSManagedObjectContext executeFetchRequest:error:] + 548
21  CoreData                            0x000000010f0e154f -[NSManagedObjectContext(_NestedContextSupport) _parentObjectsForFetchRequest:inContext:error:] + 431
22  CoreData                            0x000000010f1626b3 __82-[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:]_block_invoke + 611
23  CoreData                            0x000000010f0e133d internalBlockToNSManagedObjectContextPerform + 93
24  libdispatch.dylib                   0x0000000112b8b0cd _dispatch_client_callout + 8
25  libdispatch.dylib                   0x0000000112b6d5e5 _dispatch_barrier_sync_f_slow_invoke + 617
26  libdispatch.dylib                   0x0000000112b8b0cd _dispatch_client_callout + 8
27  libdispatch.dylib                   0x0000000112b6b8d6 _dispatch_main_queue_callback_4CF + 406
28  CoreFoundation                      0x000000010f5594f9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
29  CoreFoundation                      0x000000010f51ef8d __CFRunLoopRun + 2205
30  CoreFoundation                      0x000000010f51e494 CFRunLoopRunSpecific + 420
31  GraphicsServices                    0x0000000113b72a6f GSEventRunModal + 161
32  UIKit                               0x000000010f9b8f34 UIApplicationMain + 159
33  BlogClient                          0x000000010e5a8bdf main + 111
34  libdyld.dylib                       0x0000000112bd768d start + 1
35  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。传递Int64会混淆argumentArray,将其转换为Int可以解决问题。

这似乎是作为雷达28749194提交的NSPredicate()中的一个错误

let id:Int64 = 5785905063264256 // Making this Int works

@objc class Test:NSObject {
    let postid = id
}

let predicate = NSPredicate(format: "postid == %@"
, argumentArray: [id])

let mytest = Test()
predicate.evaluate(with: mytest) //false but should be true