Search code examples
ioscore-datarubymotion

NSFetchedResultsController created, but sections is nil


I ran into what I believe is a defect, but I'm not sure if it's in my code, or in RubyMotion, or in iOS...

"tag" model:

class Tag < CDQManagedObject
  def sectionIdentifier
    puts "Called"
    name ? name[0] : " "
  end

  def keyPathsForValuesAffectingSectionIdentifier
    NSSet.setWithObject("name")
  end
end

Part of my controller's viewDidLoad method:

self.query = Tag.sort_by(:name)
self.tags_results = NSFetchedResultsController.alloc.initWithFetchRequest(self.query.fetch_request, managedObjectContext: cdq.contexts.current, sectionNameKeyPath: "sectionIdentifier", cacheName: nil)
self.tags_results.delegate = self
puts tags_results.sections.inspect

The problem is, tags_results.sections is nil. I have something wrong here, but I am not sure where.


Solution

  • Hmm...

    I've never used rubymotion before so not 100% certain.

    However, if that is all the code you are using then I think I see the problem.

    The NSFetchedResultsController is created and you give it the request, sectionIdentifier, etc...

    However, you are not telling the controller to actually run the fetch.

    In Objective-C I would do...

    [fetchedResultsController performFetch:nil];
    

    This tells the controller to go and actually get the results.

    Only then is the sections property filled.

    The sections property is an array of the actual fetched objects so without fetching them it will always be nil.

    EDIT

    Yes...

    From the documentation... http://www.rubymotion.com/developer-center/api/NSFetchedResultsController.html#performFetch%3A-instance_method

    You need to run...

    self.tags_results.performFetch(nil)
    // or whatever the syntax is