Roobiq / RBQFetchedResultsController

Drop-in replacement for NSFetchedResultsController backed by Realm.
MIT License
476 stars 70 forks source link

Sectioning support for NSDate #58

Open smolskyaleksey opened 8 years ago

smolskyaleksey commented 8 years ago

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedDate isEqualToString:]: unrecognized selector sent to instance 0xe41bc46f25000000' * First throw call stack: ( 0 CoreFoundation 0x000000010a2cfe65 exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000109927deb objc_exception_throw + 48 2 CoreFoundation 0x000000010a2d848d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 3 CoreFoundation 0x000000010a22590a __forwarding** + 970 4 CoreFoundation 0x000000010a2254b8 _CF_forwarding_prep_0 + 120 5 FoilChat 0x0000000105cd4e31 -[RBQFetchedResultsController createCacheWithRealm:cacheName:forFetchRequest:sectionNameKeyPath:] + 1345 6 FoilChat 0x0000000105ccf5eb -[RBQFetchedResultsController performFetch] + 587 7 FoilChat 0x0000000105bd309b -[FCChatDataSource initWithCollectionView:forChat:] + 1051 8 FoilChat 0x0000000105c1e8b4 -[FCChatViewController viewDidLoad] + 724 9 UIKit 0x0000000107c95f98 -[UIViewController loadViewIfRequired] + 1198 10 UIKit 0x0000000107c9bf4f -[UIViewController viewWillAppear:] + 120 11 UIKit 0x0000000107ccbe44 -[UINavigationController _startCustomTransition:] + 1203 12 UIKit 0x0000000107cdc23f -[UINavigationController _startDeferredTransitionIfNeeded:] + 712 13 UIKit 0x0000000107cdd3af -[UINavigationController __viewWillLayoutSubviews] + 57 14 UIKit 0x0000000107e83ff7 -[UILayoutContainerView layoutSubviews] + 248 15 UIKit 0x0000000107bb64a3 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703 16 QuartzCore 0x000000010788059a -[CALayer layoutSublayers] + 146 17 QuartzCore 0x0000000107874e70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 18 QuartzCore 0x0000000107874cee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 19 QuartzCore 0x0000000107869475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277 20 QuartzCore 0x0000000107896c0a _ZN2CA11Transaction6commitEv + 486 21 UIKit 0x0000000107b2ab47 _afterCACommitHandler + 174 22 CoreFoundation 0x000000010a1fb367 CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 23 23 CoreFoundation 0x000000010a1fb2d7 CFRunLoopDoObservers + 391 24 CoreFoundation 0x000000010a1f0f2b CFRunLoopRun + 1147 25 CoreFoundation 0x000000010a1f0828 CFRunLoopRunSpecific + 488 26 GraphicsServices 0x000000010c9b1ad2 GSEventRunModal + 161 27 UIKit 0x0000000107aff610 UIApplicationMain + 171 28 FoilChat 0x0000000105be653f main + 111 29 libdyld.dylib 0x000000010aad992d start + 1 )

I have this one when I try to group datasource by NSDate. I have property "date" (NSDate type) and i setup it to sectionNameKeyPath like @"date".

Why do you compare NSString in this lines ..... if (sectionNameKeyPath) {

            // Check your sectionNameKeyPath if a crash occurs...
            NSString *sectionTitle = [object valueForKeyPath:sectionNameKeyPath];

            // New Section Found --> Process It
            if (![sectionTitle isEqualToString:currentSectionTitle]) {

....

bigfish24 commented 8 years ago

Yeah right now the sectioning isn't designed to work with NSDate. Conceptually, I would like to know what you were expecting the grouping to be... based on the hour/day/week?

smolskyaleksey commented 8 years ago

I want to group some objects by date. Each object has field where i store NSDate without time. For example 15-01-2016 21:00:00 GMT +3

bigfish24 commented 8 years ago

Well the library doesn't support this so instead you should convert the date into its text representation like: "Today" or "Tomorrow" and store that as a string. You can then just have logic to update these values every time the user opens the app. In addition, iOS then has a notification for significant time change, so that you can update all the text strings of the user if he/she is using the app across midnight.

smolskyaleksey commented 8 years ago

I think it is bad solution. In my vision, the best solution, store date like string "18-01-16" and group entity by date in string format and then convert string to date via NSDateFormater.

bigfish24 commented 8 years ago

Fair enough, I know it isn't ideal, but I don't currently have time to add date support like you describe. If you have time to try it yourself, would appreciate it.

koloritcm commented 8 years ago

@bigfish24 @smolskyaleksey I just ran into the same kind of problem, but with boolean. It worked perfectly with NSFetchedResultsController. There I received "0" and "1" to the titleForHeaderInSection so I could choose my titles for these.

Could we convert non-string types to strings somewhere in the process?

koloritcm commented 8 years ago

I think it works just changing the two places the section name is fetched:

[object valueForKeyPath:sectionNameKeyPath] to [[object valueForKeyPath:sectionNameKeyPath] description]

inPhilly commented 7 years ago

@bigfish24 I haven't quite figured out how to do a pull request or I would try gunnarblom's solution. It seems a very logical solution and would alleviate the necessity of producing an entirely new field for each sectionNameKeyPath that is a date or a number. I don't see any downside to it since NSString description yields the original NSString. Would you consider making this change to the code that is available through Carthage? I don't know if this repository is being actively maintained, but this change would be extremely, extremely helpful. Thanks.