Fetchedresultscontroller and Core Data

Source: Internet
Author: User

Nsfetchedresultscontroller is designed to allow the view to respond to changes in the model layer in a timely manner, and more specifically to refresh the data on the UITableView.


Generally speaking, An instance of Nsfetchedresultscontroller is initialized and held by an instance of Uitableviewcontroller.

Once created, we will assign it an agent (Nsfetchedresultscontrollerdelegate) and notify the corresponding TableView by proxy when the model in core data changes.


  1. new objects: New objects cannot be associated to Fetchedresultscontroller, typically by [nsentitydescription Insertnewobjectforentityforname:@ "Post" Inmanagedobjectcontext:Managedobjectcontext]To Initialize and assign a value, and save ( [Managedobjectcontext Save: &error]), where a consistent managedobjectcontext is used, triggering a nsfetchedresultschangeinsert notification.

  2. Updates to objects: In some requirements, the object may be asynchronously getting JSON data in the background, Then synchronize into the database. Direct Update the object and save it ( Span style= "FONT-SIZE:11PX;FONT-FAMILY:MENLO;" >[ save ).

  3. Object deletion: The Nsfetchedresultschangedelete notification is triggered by deleting the corresponding model object on a consistent managedobjectcontext. And the objects associated with their cascade relationships are deleted together, eliminating the need for separate operations. [post. Managedobjectcontext DeleteObject :p OST];

The official recommended way is to handle the update of section or row on TableView as much as possible in the agent's Didchangeobject method. Whether the operation of the object is obtained by Nsarray or Nsfetchedresultscontroller, or by the Insertnewobjectforentityforname method to add, The Didchangeobject method in the proxy is triggered whenever an object in the result set contained by the Controller object that is bound to the proxy changes.


For object-related object changes, such as the Post object has an Owner object relationship, owner has changed, if it will affect the result set, the same will trigger the post Didchangeobject method, and vice versa.

The most frequently encountered problem is that the relevant model has been updated and saved in context, but the associated agent's Didchangeobject method has not been triggered and summarized for several reasons:

1. Inconsistent Managedobejctcontext

2. Analyze whether the predicate of the fetch to controller contains the affected model, and check that it corresponds to the new/update/delete operation in the result set after the change of the relevant property of the model. Then the corresponding nsfetchedresultschange will be triggered in the didchangeobject.

3. The delegate of the currently returned controller is bound to the corresponding agent.

Related information:

Https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/nsfetchedresultscontroller.html


This article is from "Leo's Technical Diary" blog, please be sure to keep this source http://shskey.blog.51cto.com/8310114/1784959

Fetchedresultscontroller and Core Data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.