Skip to content
This repository has been archived by the owner on Jun 2, 2018. It is now read-only.

Add type safe Entity Monitor #52

Merged
merged 16 commits into from
Nov 20, 2015
Merged

Add type safe Entity Monitor #52

merged 16 commits into from
Nov 20, 2015

Conversation

rcedwards
Copy link
Contributor

EntityMonitor observes a given NSManagedObjectContext for Updates, Deletes, and Inserts of a specific entity type. An optional filter predicate can also be added.

This can be used in place of an NSFetchedResultsController when you are not binding your data to an UITableViewController.

*/
static public func newInContext(context: NSManagedObjectContext) -> Self {
return NSEntityDescription.insertNewObjectForEntityForName(entityName, inManagedObjectContext: context) as! Self
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've had success with this as an initializer:

extension CoreDataModelable {
    static func entityInContext(context: NSManagedObjectContext) -> NSEntityDescription! {
        guard let entity = NSEntityDescription.entityForName(entityName, inManagedObjectContext: context) else {
            assertionFailure("Entity named \(entityName) doesn't exist. Fix the entity description or naming of \(Self.self).")
            return nil
        }
        return entity
    }
}

extension CoreDataModelable where Self: NSManagedObject {
    init(managedObjectContext context: NSManagedObjectContext) {
        self.init(entity: Self.entityInContext(context), insertIntoManagedObjectContext: context)
    }
}

*/
static public func findFirst(predicate: NSPredicate?, context: NSManagedObjectContext) -> Self? {
let fetchRequest = NSFetchRequest(entityName: entityName)
fetchRequest.predicate = predicate
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add fetchRequest.fetchLimit = 1 here to improve performance.

Rather than have the test case conform to EntityMonitorDelegate there are
two distinct classes for each test type.

This also shows an example of having EntityMonitors for two different Entity types.
This was an early attempt at triggering the MOC to observe object changes.

Earlier addition of moc.processPendingChanges()  is much more explicit and deterministic.
This ensures that if an entity is not in a supplied context it will assert with a useful failure
	rather than failing to force cast.
Including subentries is unnecessary since we're deleting objects.

Also adding a note on how we could improve the performance using a batch delete request.
Limiting the batch and fetch size to a single result and returning the object already faulted
@rcedwards
Copy link
Contributor Author

Updated based on all the comments. @lyricsboy and @zwaldowski want to take another pass? thanks.

import CoreData
import CoreDataStack

var insertExpectation: XCTestExpectation!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer to see these as instance variables and passed through initializers to the delegate classes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I considered that but that will not work for the way expectations are setup for the OnChange notification tests.

https://github.com/bignerdranch/CoreDataStack/pull/52/files#diff-d71f30256eb2d222f35c00e211ba053aR120

They need to be setup and waited on in a serial fashion so they cannot be setup in the delegate's initializer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean now. We can proceed without this change.

lyricsboy pushed a commit that referenced this pull request Nov 20, 2015
@lyricsboy lyricsboy merged commit 7779eac into master Nov 20, 2015
@lyricsboy lyricsboy deleted the rcedwards/object_sentry branch November 20, 2015 19:03
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants