Skip to content

Commit

Permalink
Fix issues with previous PR
Browse files Browse the repository at this point in the history
  • Loading branch information
kreeger committed Mar 22, 2019
1 parent 45f999f commit 67f8e6a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Example/AutomaticViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AutomaticViewController: UIViewController {

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
dataSource.execute()
dataSource.startListening()
}


Expand Down
3 changes: 2 additions & 1 deletion Flapjack/Core/DataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ public protocol DataSource {
/**
This stub implementation of `DataSource` makes it relatively easy to build your own by only having to implement a few
short properties/functions, especially if you're managing a non-grouped set of objects. Simply implement `allObjects`
and `execute()`, make sure you call `onChange` when the data set changes, and everything else should hinge on that.
and `startListening()`, make sure you call `onChange` when the data set changes, and everything else should hinge on
that.
*/
public extension DataSource {
/// If not implemented, this returns the `count` of `allObjects`.
Expand Down
2 changes: 1 addition & 1 deletion Flapjack/CoreData/SingleCoreDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public class SingleCoreDataSource<T: NSManagedObject & DataObject>: NSObject, Si

// Store our newly-minted context, and refetch.
self.context = newContext
execute()
startListening()
}

@objc
Expand Down
22 changes: 11 additions & 11 deletions Tests/CoreData/CoreDataSourceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class CoreDataSourceTests: XCTestCase {

func testObjectsArePopulatedAfterExecution() {
let dataSource = CoreDataSource<MockEntity>(dataAccess: dataAccess)
dataSource.execute()
dataSource.startListening()
XCTAssertEqual(dataSource.numberOfObjects, 5)
XCTAssertEqual(dataSource.allObjects.count, 5)
XCTAssertEqual(dataSource.numberOfSections, 1)
Expand Down Expand Up @@ -88,7 +88,7 @@ class CoreDataSourceTests: XCTestCase {
XCTAssertEqual(items.count, 2)
XCTAssertEqual(sections.count, 0)
}
dataSource.execute()
dataSource.startListening()
_ = dataAccess.mainContext.create(MockEntity.self, attributes: ["someProperty": "someValue alpha"])
dataAccess.mainContext.destroy(object: entityOne)
dataAccess.mainContext.persist()
Expand All @@ -100,7 +100,7 @@ class CoreDataSourceTests: XCTestCase {

func testFilteredFetchOnlyReturnsThoseMatching() {
let dataSource = CoreDataSource<MockEntity>(dataAccess: dataAccess, attributes: ["someProperty": "someValue alpha"])
dataSource.execute()
dataSource.startListening()
XCTAssertEqual(dataSource.numberOfObjects, 2)
XCTAssertEqual(dataSource.allObjects.count, 2)
XCTAssertTrue(dataSource.allObjects.contains(entityOne))
Expand All @@ -111,7 +111,7 @@ class CoreDataSourceTests: XCTestCase {

func testFilteredAutomaticallyUpdatesAllObjectsOnChange() {
let dataSource = CoreDataSource<MockEntity>(dataAccess: dataAccess, attributes: ["someProperty": "someValue alpha"])
dataSource.execute()
dataSource.startListening()
XCTAssertEqual(dataSource.numberOfObjects, 2)
XCTAssertEqual(dataSource.allObjects.count, 2)

Expand All @@ -123,7 +123,7 @@ class CoreDataSourceTests: XCTestCase {

func testFilteredAutomaticallyUpdatesAllObjectsOnDestroy() {
let dataSource = CoreDataSource<MockEntity>(dataAccess: dataAccess, attributes: ["someProperty": "someValue alpha"])
dataSource.execute()
dataSource.startListening()
XCTAssertEqual(dataSource.numberOfObjects, 2)
XCTAssertEqual(dataSource.allObjects.count, 2)
dataAccess.mainContext.destroy(object: entityOne)
Expand All @@ -134,7 +134,7 @@ class CoreDataSourceTests: XCTestCase {

func testFilteredAutomaticallyIncludesNewMatchingObjects() {
let dataSource = CoreDataSource<MockEntity>(dataAccess: dataAccess, attributes: ["someProperty": "someValue alpha"])
dataSource.execute()
dataSource.startListening()
XCTAssertEqual(dataSource.numberOfObjects, 2)
XCTAssertEqual(dataSource.allObjects.count, 2)

Expand All @@ -150,15 +150,15 @@ class CoreDataSourceTests: XCTestCase {

func testSortedReturnsObjectsInProperOrder() {
let dataSource = CoreDataSource<MockEntity>(dataAccess: dataAccess, sorters: [SortDescriptor("someProperty", ascending: false)])
dataSource.execute()
dataSource.startListening()
// entityOne and entityTwo could be in either position 1 or 2
XCTAssertEqual(dataSource.allObjects.first, entityFour)
XCTAssertEqual(dataSource.allObjects.last, entityFive)
}

func testSortedReturnsObjectsInProperOrderEvenWithNewItemsOrChanges() {
let dataSource = CoreDataSource<MockEntity>(dataAccess: dataAccess, sorters: [SortDescriptor("someProperty", ascending: false)])
dataSource.execute()
dataSource.startListening()
XCTAssertEqual(dataSource.allObjects.first, entityFour)
XCTAssertEqual(dataSource.allObjects.last, entityFive)

Expand All @@ -184,7 +184,7 @@ class CoreDataSourceTests: XCTestCase {
XCTAssertEqual(sections.count, 3)
}

dataSource.execute()
dataSource.startListening()

XCTAssertEqual(dataSource.numberOfObjects, 5)
XCTAssertEqual(dataSource.numberOfSections, 4)
Expand All @@ -207,7 +207,7 @@ class CoreDataSourceTests: XCTestCase {

func testLimitedOnlyReturnsThoseFallingInUnderTheLimit() {
let dataSource = CoreDataSource<MockEntity>(dataAccess: dataAccess, sorters: [SortDescriptor("someProperty", ascending: false)], limit: 2)
dataSource.execute()
dataSource.startListening()
XCTAssertEqual(dataSource.numberOfObjects, 2)
XCTAssertEqual(dataSource.allObjects.count, 2)
XCTAssertFalse(dataSource.allObjects.contains(entityOne))
Expand All @@ -219,7 +219,7 @@ class CoreDataSourceTests: XCTestCase {

func testLimitedAutomaticallyUpdatesAllObjectsOnChange() {
let dataSource = CoreDataSource<MockEntity>(dataAccess: dataAccess, sorters: [SortDescriptor("someProperty", ascending: false)], limit: 2)
dataSource.execute()
dataSource.startListening()
XCTAssertEqual(dataSource.numberOfObjects, 2)
XCTAssertEqual(dataSource.allObjects.count, 2)

Expand Down
20 changes: 10 additions & 10 deletions Tests/CoreData/SingleCoreDataSourceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ class SingleCoreDataSourceTests: XCTestCase {

func testExecutionFetchesRightAway() {
XCTAssertNil(dataSource.object)
dataSource.execute()
dataSource.startListening()
XCTAssertEqual(dataSource.object, entity)
}

func testCoreDataNotificationNotPickedUpBeforeExecute() {
func testCoreDataNotificationNotPickedUpBeforestartListening() {
let expect = expectation(description: "did change block")
expect.isInverted = true
dataSource.onChange = { object in
Expand All @@ -68,7 +68,7 @@ class SingleCoreDataSourceTests: XCTestCase {
dataSource.onChange = { object in
expect.fulfill()
}
dataSource.execute()
dataSource.startListening()
waitForExpectations(timeout: 0.5) { XCTAssertNil($0) }
}

Expand All @@ -78,7 +78,7 @@ class SingleCoreDataSourceTests: XCTestCase {
dataSource.onChange = { object in
expect.fulfill()
}
dataSource.execute()
dataSource.startListening()
entity.someProperty = "some new value"
dataAccess.mainContext.persist()
waitForExpectations(timeout: 0.5) { XCTAssertNil($0) }
Expand All @@ -90,7 +90,7 @@ class SingleCoreDataSourceTests: XCTestCase {
dataSource.onChange = { object in
expect.fulfill()
}
dataSource.execute()
dataSource.startListening()
entity.someProperty = "some new value"
dataAccess.mainContext.processPendingChanges()
waitForExpectations(timeout: 0.5) { XCTAssertNil($0) }
Expand All @@ -105,7 +105,7 @@ class SingleCoreDataSourceTests: XCTestCase {
dataSource.onChange = { object in
expect.fulfill()
}
dataSource.execute()
dataSource.startListening()
entity.someProperty = "some new value"
otherEntity.someProperty = "other other value"
dataAccess.mainContext.persist()
Expand All @@ -116,7 +116,7 @@ class SingleCoreDataSourceTests: XCTestCase {
dataAccess.mainContext.destroy(object: entity)
dataAccess.mainContext.persist()

dataSource.execute()
dataSource.startListening()
XCTAssertNil(dataSource.object)
}

Expand All @@ -126,7 +126,7 @@ class SingleCoreDataSourceTests: XCTestCase {
dataSource.onChange = { object in
expect.fulfill()
}
dataSource.execute()
dataSource.startListening()
dataAccess.mainContext.destroy(object: entity)
waitForExpectations(timeout: 0.5) { XCTAssertNil($0) }
XCTAssertNil(dataSource.object)
Expand All @@ -141,7 +141,7 @@ class SingleCoreDataSourceTests: XCTestCase {
dataSource.onChange = { object in
expect.fulfill()
}
dataSource.execute()
dataSource.startListening()
XCTAssertNil(dataSource.object)

let newEntity = dataAccess.mainContext.create(MockEntity.self, attributes: attributes)
Expand All @@ -159,7 +159,7 @@ class SingleCoreDataSourceTests: XCTestCase {
dataSource.onChange = { object in
expect.fulfill()
}
dataSource.execute()
dataSource.startListening()

entity.someProperty = "some new value"
newEntity.someProperty = attributes["someProperty"]
Expand Down

0 comments on commit 67f8e6a

Please sign in to comment.