AsyncDisplayKit icon indicating copy to clipboard operation
AsyncDisplayKit copied to clipboard

[ASRangeController] Range callback methods aren't always called for replacing nodes at existing paths.

Open Adlai-Holler opened this issue 11 years ago • 5 comments

EDIT: Issue is more general than this, see next comment. I noticed a bug in ASTableView. I have a table view that has two very different modes, and I use reloadDataImmediately to switch between them. I noticed that when I switch to a mode that has more rows, fetchData is generally called on the new nodes, but when I switch to a mode with fewer rows, it isn't.

Digging deeper I saw that even after reloadData, ASRangeController still regards the old index paths as handled, so post-update, in updateVisibleNodeIndexPaths I would expect addedIndexPaths to contain all the index paths in the range but instead it subtracts the index paths that were in the range pre-update.

I'm about to submit a pull request that fixes this issue for me. Not being a range controller expert, it may be flawed, but there you have it.

Adlai-Holler avatar Nov 18 '15 22:11 Adlai-Holler

Actually, I think this issue is more general than just reloadData – something like the range controller only cares about index paths and not their contents.

I've currently got a setup where I use a section with 1 cell to hold my "Loading…" placeholder. Then when the content comes in, I submit a batch update that contains "remove section 0, insert section 0". The first cell in the new section never gets a fetchData call. All other cells do. As if the range controller said "ah! I've already handled (0,0)" but it didn't realize that (0,0) had its contents replaced. Going to attempt to reduce this down into a test case, shouldn't be too hard.

Adlai-Holler avatar Nov 19 '15 18:11 Adlai-Holler

@Adlai-Holler this explains very well my issue, I have duplicate nodes and sometimes incorrect indexPaths after having fetched a second page of data.

1nput0utput avatar Nov 19 '15 19:11 1nput0utput

@Adlai-Holler solid debugging here — a very clear explanation of the problem. I think this is strong support for the impression that ASRangeController needs its first big refactor, as today its core logic is from the same era as the original ASDataController (!!!).

@1nput0utput could you share any details on the issue you encountered? Was your case specific to reloadData (and thus fixed by the patch linked above), or is the editing scenario described later the one you are seeing?

As per usual with these issues, not having a test case is one inhibition to fixing with confidence. I'm actively mucking about in ASRangeController (and @levi too, who did a sweeping cleanup of naming and organization of the file in prep for deeper changes). But with the weekend dwindling, I may not be able to fix this in time for 1.9.2...

The PR I just landed, with ASInterfaceState, holds the potential to help us fix this. It makes nodes tolerant to "sloppy" behavior of a class like ASRangeController, in that if the class were too chatty and told it to enter or exit a state multiple times, it would short-circuit and not actually cause errant calls to fetchData / clearContents / etc (this was not the case before the patch). So if we found the right point at which to do a reconciliation (setting the intended state on all the nodes involved), it could actually be pretty efficient.

The ideal refactor for ASRangeController will be to eliminate ASRangeHandler entirely, and call setInterfaceState only once per node on a given pass. This would particularly optimize the case being described here where new / replacement nodes may need to have multiple elements of the state set (such as visible, display, fetchData).

appleguy avatar Nov 30 '15 01:11 appleguy

@appleguy @Adlai-Holler Here's the crash I get beside the duplicates, which seems to happen mostly on iOS 8 for some reason. The crash is happening when the app is brought in foreground. I don't use reloadData, I let the fetchedResultController do the job. I am currently using the master branch.

-[__NSArrayM objectAtIndex:]: index 15 beyond bounds [0 .. 14]

Thread : Fatal Exception: NSRangeException
0  CoreFoundation                 0x1846a422c __exceptionPreprocess
1  libobjc.A.dylib                0x1963180e4 objc_exception_throw
2  CoreFoundation                 0x1845874fc -[__NSArrayM removeObjectAtIndex:]
3  AsyncDisplayKit                0x1008179d4 -[ASDataController nodeAtIndexPath:] (ASDataController.mm:905)
4  AsyncDisplayKit                0x100843428 -[ASTableView nodeForRowAtIndexPath:] (ASTableView.mm:370)
5  fooapp                            0x100134da8 specialized HomeViewController.controller(NSFetchedResultsController, didChangeObject : AnyObject, atIndexPath : NSIndexPath?, forChangeType : NSFetchedResultsChangeType, newIndexPath : NSIndexPath?) -> () (HomeViewController.swift:272)
6  fooapp                            0x1001330dc @objc HomeViewController.controller(NSFetchedResultsController, didChangeObject : AnyObject, atIndexPath : NSIndexPath?, forChangeType : NSFetchedResultsChangeType, newIndexPath : NSIndexPath?) -> () (HomeViewController.swift)
7  CoreData                       0x18446ad30 __77-[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:]_block_invoke
8  CoreData                       0x1843e9218 developerSubmittedBlockToNSManagedObjectContextPerform
9  CoreData                       0x1843e90e4 -[NSManagedObjectContext performBlockAndWait:]
10 CoreData                       0x184374650 -[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:]
11 CoreFoundation                 0x18464a010 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__
12 CoreFoundation                 0x1845870f0 _CFXNotificationPost
13 Foundation                     0x1854b6a80 -[NSNotificationCenter postNotificationName:object:userInfo:]
14 CoreData                       0x1843745ac -[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:]
15 CoreData                       0x18437452c -[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:withDeletions:withUpdates:withRefreshes:]
16 CoreData                       0x184391208 -[NSManagedObjectContext(_NSInternalChangeProcessing) _postRefreshedObjectsNotificationAndClearList]
17 CoreData                       0x184372f8c -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:]
18 CoreData                       0x1843e8c10 -[NSManagedObjectContext _mergeChangesFromDidSaveDictionary:usingObjectIDs:]
19 CoreData                       0x1843e8f1c -[NSManagedObjectContext mergeChangesFromContextDidSaveNotification:]
20 MagicalRecord                  0x100ca09f4 +[NSManagedObjectContext(MagicalRecord) rootContextDidSave:] (NSManagedObjectContext+MagicalRecord.m:211)
21 libdispatch.dylib              0x196995994 _dispatch_call_block_and_release
22 libdispatch.dylib              0x196995954 _dispatch_client_callout
23 libdispatch.dylib              0x19699a20c _dispatch_main_queue_callback_4CF
24 CoreFoundation                 0x18465b544 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__
25 CoreFoundation                 0x1846595ec __CFRunLoopRun
26 CoreFoundation                 0x184584f74 CFRunLoopRunSpecific
27 GraphicsServices               0x18dfe76fc GSEventRunModal
28 UIKit                          0x189186d94 UIApplicationMain
29 fooapp                            0x1000e4a80 main (AppDelegate.swift:17)
30 libdyld.dylib                  0x1969c2a08 start

1nput0utput avatar Dec 04 '15 16:12 1nput0utput

@Adlai-Holler @1nput0utput I'm nearly certain this is fixed in 1.9.6. Please test the crap out of it!

appleguy avatar Jan 26 '16 05:01 appleguy