iphone - NSFetchedResultsController - phantom row -
just run tricky nsfetchedresultscontroller problem.
the following code works fine in cases except first entry core data database when reports 2 rows!
id <nsfetchedresultssectioninfo> sectioninfo = [[fetchedresultscontroller sections] objectatindex:section]; numberofrows = [sectioninfo numberofobjects];
if add additional entries these reported correctly. , if delete both of 2 initial rows works fine.
any suggestions? if it's help, using:
-com.apple.coredata.sqldebug 1
i can see there 1 insert , 2 selects.
also, problem doesn't seem happen if i've visited view containing nsfetchedresultscontroller code (i.e. before doing inserts).
====
update 1:
wonder if walk through code help...
1. viewcontroller starts background nsoperation download
2. when completes sends notification appdelegate
3. when appdelegate gets notification imports data (doing check make sure it's on main thread before doing so)
here's relevant bit of importerdidsave code (where technique taken from):
- (void)importerdidsave:(nsnotification *)savenotification { nslog(@"in importerdidsave..."); if ([nsthread ismainthread]) { nslog(@"... on main thread."); nslog(@"number of nsfetchedresultscontroller rows before: %d", [[[fetchedresultscontroller sections] objectatindex:0] numberofobjects]); [self.managedobjectcontext mergechangesfromcontextdidsavenotification:savenotification]; nslog(@"number of nsfetchedresultscontroller rows after: %d", [[[fetchedresultscontroller sections] objectatindex:0] numberofobjects]);
which outputs:
... on main thread. number of nsfetchedresultscontroller rows before: 1 number of nsfetchedresultscontroller rows after: 3
don't rely on direct sql debugging. core data's use of sql buried behind objects (it doesn't use sql time) can't tell state of object graph looking @ sql.
most likely, have 2 objects in section 1 in memory , 1 in persistent store. persisted object apparent in sql debug. have thousands of objects in graph , thousands of rows in table section until objects persisted, won't show in sql. moreover, pattern of sql inserts , deletes not directly reflect insertions , deletes of object graph.
in short, sql debug pretty useless debugging core data , should ignore in vast majority of cases.
if fetched results controller reports 2 objects in section there 2 objects in section. object came from, can't without more information.
check:
- your section key if any. if sections misconfigured have rows don't expect them.
- check insertion see if duplicating insertions.
you can confirm whether frc correct manually performing same fetch uses , seeing gets returned. sort return on each section key , can see how many objects appear in section.
Comments
Post a Comment