{"action":"created","issue":{"url":"https://api.github.com/repos/couchbase/couchbase-lite-ios/issues/741","labels_url":"https://api.github.com/repos/couchbase/couchbase-lite-ios/issues/741/labels{/name}","comments_url":"https://api.github.com/repos/couchbase/couchbase-lite-ios/issues/741/comments","events_url":"https://api.github.com/repos/couchbase/couchbase-lite-ios/issues/741/events","html_url":"https://github.com/couchbase/couchbase-lite-ios/issues/741","id":82324914,"number":741,"title":"CBLIncrementalStore and NSDate","user":{"login":"paolonl","id":199377,"avatar_url":"https://avatars.githubusercontent.com/u/199377?v=3","gravatar_id":"","url":"https://api.github.com/users/paolonl","html_url":"https://github.com/paolonl","followers_url":"https://api.github.com/users/paolonl/followers","following_url":"https://api.github.com/users/paolonl/following{/other_user}","gists_url":"https://api.github.com/users/paolonl/gists{/gist_id}","starred_url":"https://api.github.com/users/paolonl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/paolonl/subscriptions","organizations_url":"https://api.github.com/users/paolonl/orgs","repos_url":"https://api.github.com/users/paolonl/repos","events_url":"https://api.github.com/users/paolonl/events{/privacy}","received_events_url":"https://api.github.com/users/paolonl/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-05-29T08:12:51Z","updated_at":"2015-05-29T16:25:00Z","closed_at":null,"body":"Hi guys,\r\n\r\nI am having an issue with a CoreData fetch using NSDates on CBLInrementalStore 1.0.4\r\nI have an entity Menu which has a `date` property modelled as NSDate. I do have the need to fetch a a menu for a given date.\r\nMy fetch method looks like this \r\n\r\n```swift\r\nfunc menuForDate(date: NSDate) -> Menu? {\r\n      var todayMidnight : NSDate = .... // the midnight before the passed date\r\n      var tomorrowMidnight : NSDate = .... // the midnight after the passed date\r\n      \r\n        let fetchRequest = NSFetchRequest(entityName: \"Menu\")\r\n        fetchRequest.predicate = NSPredicate(format: \"%@ <= date AND %@ >= date\", startDate, endDate)\r\n        let menu: Menu? = PPSPersistenceController.sharedController.mainObjectContext.executeFetchRequest(fetchRequest, error: nil)?.first as? Menu\r\n        return menu\r\n}\r\n```\r\n\r\nFrom CoreData point of view, that is a valid predicate. But CBLIncrementalStore throws me this exception \r\n\r\n```\r\n2015-05-29 09:49:20.208 PPStaff[3566:33760] -[__NSCFString timeIntervalSinceReferenceDate]: unrecognized selector sent to instance 0x7fcdc8da5360\r\n2015-05-29 09:49:34.234 PPStaff[3566:33760] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString timeIntervalSinceReferenceDate]: unrecognized selector sent to instance 0x7fcdc8da5360'\r\n*** First throw call stack:\r\n(\r\n0   CoreFoundation                      0x000000010d18bc65 __exceptionPreprocess + 165\r\n1   libobjc.A.dylib                     0x000000010ca68bb7 objc_exception_throw + 45\r\n2   CoreFoundation                      0x000000010d1930ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205\r\n3   CoreFoundation                      0x000000010d0e913c ___forwarding___ + 988\r\n4   CoreFoundation                      0x000000010d0e8cd8 _CF_forwarding_prep_0 + 120\r\n5   CoreFoundation                      0x000000010d0c8de3 -[NSDate compare:] + 51\r\n6   Foundation                          0x000000010c65534b -[NSComparisonPredicateOperator performPrimitiveOperationUsingObject:andObject:] + 365\r\n7   Foundation                          0x000000010c6233f0 -[NSPredicateOperator performOperationUsingObject:andObject:] + 285\r\n8   Foundation                          0x000000010c622af2 -[NSComparisonPredicate evaluateWithObject:substitutionVariables:] + 313\r\n9   PPStaff                             0x000000010b677ac4 -[CBLIncrementalStore _evaluatePredicate:withEntity:properties:] + 1924\r\n10  PPStaff                             0x000000010b677687 -[CBLIncrementalStore _evaluatePredicate:withEntity:properties:] + 839\r\n11  PPStaff                             0x000000010b672abf -[CBLIncrementalStore _filterObjectsOfEntity:fromQuery:byFetchRequest:inContext:error:] + 1119\r\n12  PPStaff                             0x000000010b67259c -[CBLIncrementalStore _queryObjectsOfEntity:byFetchRequest:inContext:error:] + 924\r\n13  PPStaff                             0x000000010b66df4e -[CBLIncrementalStore executeRequest:withContext:error:] + 8014\r\n14  CoreData                            0x000000010cd83a6c __65-[NSPersistentStoreCoordinator executeRequest:withContext:error:]_block_invoke + 3356\r\n15  CoreData                            0x000000010cd8cc30 gutsOfBlockToNSPersistentStoreCoordinatorPerform + 192\r\n16  libdispatch.dylib                   0x000000010f20d614 _dispatch_client_callout + 8\r\n17  libdispatch.dylib                   0x000000010f1f3002 _dispatch_barrier_sync_f_invoke + 365\r\n18  CoreData                            0x000000010cd7e245 _perform + 197\r\n19  CoreData                            0x000000010cca8a58 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 504\r\n20  CoreData                            0x000000010cca72ca -[NSManagedObjectContext executeFetchRequest:error:] + 586\r\n21  PPStaff                             0x000000010b684377 _TZFC7PPStaff4Menu11menuForDatefMS0_FCSo6NSDateGSqS0__ + 1255\r\n22  PPStaff                             0x000000010b69806a _TFC7PPStaff21PPSMenuViewController11viewDidLoadfS0_FT_T_ + 986\r\n23  PPStaff                             0x000000010b698302 _TToFC7PPStaff21PPSMenuViewController11viewDidLoadfS0_FT_T_ + 34\r\n24  UIKit                               0x000000010db6a210 -[UIViewController loadViewIfRequired] + 738\r\n25  UIKit                               0x000000010db6a40e -[UIViewController view] + 27\r\n26  UIKit                               0x000000010db8f297 -[UINavigationController _startCustomTransition:] + 633\r\n27  UIKit                               0x000000010db9b3bf -[UINavigationController _startDeferredTransitionIfNeeded:] + 386\r\n28  UIKit                               0x000000010db9bf0e -[UINavigationController __viewWillLayoutSubviews] + 43\r\n29  UIKit                               0x000000010dce6715 -[UILayoutContainerView layoutSubviews] + 202\r\n30  UIKit                               0x000000010dab9a2b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 536\r\n31  QuartzCore                          0x000000010d8bcec2 -[CALayer layoutSublayers] + 146\r\n32  QuartzCore                          0x000000010d8b16d6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380\r\n33  QuartzCore                          0x000000010d8b1546 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24\r\n34  QuartzCore                          0x000000010d81d886 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242\r\n35  QuartzCore                          0x000000010d81ea3a _ZN2CA11Transaction6commitEv + 462\r\n36  UIKit                               0x000000010da36626 _UIApplicationHandleEventQueue + 2140\r\n37  CoreFoundation                      0x000000010d0bf431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17\r\n38  CoreFoundation                      0x000000010d0b52fd __CFRunLoopDoSources0 + 269\r\n39  CoreFoundation                      0x000000010d0b4934 __CFRunLoopRun + 868\r\n40  CoreFoundation                      0x000000010d0b4366 CFRunLoopRunSpecific + 470\r\n41  GraphicsServices                    0x0000000113167a3e GSEventRunModal + 161\r\n42  UIKit                               0x000000010da39900 UIApplicationMain + 1282\r\n43  PPStaff                             0x000000010b68a857 main + 135\r\n44  libdyld.dylib                       0x000000010f241145 start + 1\r\n)\r\n```\r\n\r\nI read somewhere that CBLincrementalStore uses NSDate using ISO 8601strings. Is it maybe related to this? \r\nThanks\r\n\r\nBest\r\nP."},"comment":{"url":"https://api.github.com/repos/couchbase/couchbase-lite-ios/issues/comments/106863542","html_url":"https://github.com/couchbase/couchbase-lite-ios/issues/741#issuecomment-106863542","issue_url":"https://api.github.com/repos/couchbase/couchbase-lite-ios/issues/741","id":106863542,"user":{"login":"snej","id":43241,"avatar_url":"https://avatars.githubusercontent.com/u/43241?v=3","gravatar_id":"","url":"https://api.github.com/users/snej","html_url":"https://github.com/snej","followers_url":"https://api.github.com/users/snej/followers","following_url":"https://api.github.com/users/snej/following{/other_user}","gists_url":"https://api.github.com/users/snej/gists{/gist_id}","starred_url":"https://api.github.com/users/snej/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/snej/subscriptions","organizations_url":"https://api.github.com/users/snej/orgs","repos_url":"https://api.github.com/users/snej/repos","events_url":"https://api.github.com/users/snej/events{/privacy}","received_events_url":"https://api.github.com/users/snej/received_events","type":"User","site_admin":false},"created_at":"2015-05-29T16:25:00Z","updated_at":"2015-05-29T16:25:00Z","body":"JSON doesn't natively support dates, so the usual convention is to represent them as strings in ISO-8601 format. CBLIncrementalStore should do that mapping when storing and retrieving documents.\r\n\r\n@pasin knows CBLIncrementalStore a lot better than I do, but I just skimmed the source code and it looks like it doesn't: There is no occurrence of \"NSDate\" in CBLIncrementalStore.m. This looks like missing functionality that needs to be added. (And the same goes for NSData, which needs to be converted to/from base64.)\r\n\r\nFor now you'll need to work around this by declaring the model property as a string, and using the date-conversion methods on `CBLJSON` to convert to/from NSDate. Or instead, you can change the property type to double and use `NSDate.timeIntervalSinceReferenceDate` to convert; this is much faster but not human-readable in JSON format and not \"how things are done\" in the JSON world. (I would recommend double, since you probably don't care what your JSON docs look like and are probably not planning to make your documents interoperate with other apps.)"}}