Basics of failures and failures in many ways

I have a few “theoretical” questions about the behavior of master data related to what happens with many relationships, and when to rely on walking with respect to the parent object and when a new selection request should be created. They are all very connected.

Background

Assume a parent RPBook that has to-many relation to the RPChapter . The book has many chapters. The inverse is also specified in the underlying data model. The basic form of a manual relationship is used, so the RPChapter object has the chapterIndex property. I am not using the new ordered iOS5 relationships here (not so important for these issues).

To go to chapters in a book, use the chapters attribute:

 RPBook *myBook; // Assume this is already set to an existing RPBook NSSet *myChapters = myBook.chapters 

Usage / Setting

In the iPhone app, we start by presenting a table showing a list of RPBook instances. The relevant chapters will not be preliminarily taken into account as part of the selection specification for the resulting result controller that supports table presentation, since these chapters are not yet needed.

Now I select one of these RPBook instances, they take me to a new page, and I have this link to the RPBook instance in my view controller that does not have its chapters preprogrammed.

Question 1: Call filteredSetUsingPredicate: on chapters now

If I want to filter through the chapters relation using filteredSetUsingPredicate: directly, this will work reliably, given that I did not preselect all the associated RPChapter instances of the current RPBook Looking at? In other words, filteredSetUsingPredicate: causes errors behind the scenes of all objects in this regard to do its thing, or will it only be misleading results based on which of the chapters was already in memory (if any)?

If I don't have a glaring number of related chapters in a book, should I use this instead by calling allObjects first? those.

 [[self.chapters allObjects] filteredArrayUsingPredicate:predicate] 

instead of just:

 [self.chapters filteredSetUsingPredicate:predicate] 

Question 2: Batch extract all sections of a book

In case I have an RPBook instance, but not predefined RPChapter instances associated with it, how can I get all chapters of a book to be removed in one shot using the chapters relation? Does this [myBook.chapters allObjects] , or can I get errors from this call?

I want Core Data to RPChapter all errors in the package instead of disconnect errors for an odd RPChapter request if this RPChapter behavior of using filteredSetUsingPredicate: regarding chapters in accordance with Question 1 above.

Should I use an explicit fetch request for this? Do I have to upgrade the RPBook I already have, but this time request a query in the query so that all related chapters are also retrieved using setRelationshipKeyPathsForPrefetching: :?

This last option seems wasteful to me, b / c I already have a relationship to the scope, representing a conceptually subset of all the RPChapter instances that I would be interested in. As much as possible, I would like to just walk around the graph of objects.

Question 3: NSFetchedResultsController RPChapter instances in the same thread

Customization In this case, I have an RPBook instance, but there are no RPChapter instances associated with it (but they exist in the Store). In the same view controller, I also have an NSFetchedResultsController (FRC) RPChapter instances covered by the same book. So the same thread, the same context of the managed entity.

Is an RPChapter instance from FRC that will be the same object in memory as the RPChapter instance instance that I retrieve from myBook.chapters that has the same ObjectID ? In other words, is runtime execution of managed object queries for the same ObjectID from the same MOC in the same thread using different physical objects in memory?

Question 4: NSFetchedResultsController installation design NSFetchedResultsController inside a managed entity to serve requests for a relationship

I am trying to decide whether I should serve relationship requests, the contents of which often change (chapters in the book in my example) using the built-in chapters relation specified in my custom RPChapter managed object, or if it has ever been fine in terms of design / architecture for installing FRC instances from the RPChapter into the RPChapter managed object RPBook to efficiently serve chapter requests in this book.

This is clearly cleaner if I can just rely on the chapters accessory in myBook instance, but it seems FRC here can be more efficient and effective in situations where there are a lot of targets in many respects.

Is this redundant or is it fair to use FRC to query the RPBook about its chapters in different ways? Somehow it seems that I'm missing the opportunity to just walk on the object graph. I would like to be sure that the attitude of chapters always up to date when I download an instance of RPBook .

+7
source share
1 answer

Question 1

Yes, that will work. When you call [book chapters] , the set will be populated automatically. When you filter these objects, they will be mistaken.

However, you should use the NSFetchedResultsController here, where the predicate will be something like @ "book ==% @" instead of capturing an array.

Question 2

The best way to get NSManagedObjectContext load all the chapters is to make NSFetchRequest and configure NSFetchRequest to return fully implemented objects instead of errors. This will preload them all in one go. However, if you do not have TON chapters, you will not get big savings here.

Why?

Because when you request these chapters, even in a malfunctioning state, Core Data collects the data loading into the cache (except for a few edge cases, such as binary data), so when you "cause an error" on an object, these are just pointers to move from memory and lack of an additional disk.

You will need perhaps thousands of chapters to see any benefit from the prefetch.

Question 3

Yes. They will be the same object. NSManagedObject instances will always be shared when retrieving from the same NSManagedObjectContext . This is part of an NSManagedObjectContext job.

Question 4

You want to use the NSFetchedResultsControler , which is its job. Managing this stuff manually is wasteful and almost guaranteed less efficient than implementing Core Data.

However, the connection will always be relevant if you do not configure it from another thread. Therefore, if you do not expect updates, you can simply use an array. I would not.

+10
source

All Articles