This commit is contained in:
Pascal Edmond committed 2015-12-17 16:06:00 +01:00
1 parent e8d5d6a190
commit 8146bbca65
4 files changed
+29 -90

No files matched your search

@@ -93,16 +93,16 @@ class MasterViewController: UITableViewController, FetchedRecordsControllerDeleg
func controllerUpdate<T>(controller: FetchedRecordsController<T>, update: FetchedRecordsUpdate<T>) {
switch update {
case .Insert(_, let at):
case .Inserted(_, let at):
tableView.insertRowsAtIndexPaths([at], withRowAnimation: .Automatic)
case .Delete(_, let from):
case .Deleted(_, let from):
tableView.deleteRowsAtIndexPaths([from], withRowAnimation: .Automatic)
case .Move(_, let from, let to):
case .Moved(_, let from, let to):
tableView.moveRowAtIndexPath(from, toIndexPath: to)
case .Update(_, let at, let changes):
case .Updated(_, let at, let changes):
if let changes = changes {
let columns = ["firstName", "lastName"]
for (key, _) in changes {
@@ -1,45 +0,0 @@
//
// FetchedRecordsControllerTests.swift
// GRDB
//
// Created by Pascal Edmond on 16/12/2015.
// Copyright © 2015 Gwendal Roué. All rights reserved.
//
import XCTest
class FetchedRecordsControllerTests: GRDBTestCase {
override func setUp() {
super.setUp()
var migrator = DatabaseMigrator()
migrator.registerMigration("createPersons") { db in
try db.execute(
"CREATE TABLE persons (" +
"id INTEGER PRIMARY KEY, " +
"firstName TEXT, " +
"lastName TEXT " +
")")
}
migrator.registerMigration("addPersons") { db in
try Person(firstName: "Arthur", lastName: "Miller").insert(db)
try Person(firstName: "Barbara", lastName: "Streisand").insert(db)
try Person(firstName: "Cinderella").insert(db)
}
try! migrator.migrate(dbQueue)
}
func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
func testPerformanceExample() {
// This is an example of a performance test case.
self.measureBlock {
// Put the code you want to measure the time of here.
}
}
}
+2 -16
View File
@@ -7,9 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
315013971C21BDA400951DE3 /* FetchedRecordsControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 315013961C21BDA400951DE3 /* FetchedRecordsControllerTests.swift */; };
315013981C21BDA400951DE3 /* FetchedRecordsControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 315013961C21BDA400951DE3 /* FetchedRecordsControllerTests.swift */; };
31AEFE3F1C185E63003DB703 /* FetchedRecordsController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31AEFE3E1C185E63003DB703 /* FetchedRecordsController.swift */; };
317194AF1C2304AA002E3BDA /* FetchedRecordsController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31AEFE3E1C185E63003DB703 /* FetchedRecordsController.swift */; };
31AEFE421C185E63003DB703 /* FetchedRecordsController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31AEFE3E1C185E63003DB703 /* FetchedRecordsController.swift */; };
5606AF911C0ED37000D20487 /* libsqlite3.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 56BB86301BA98AB0001F9168 /* libsqlite3.tbd */; };
5607EFD31BB8254800605DE3 /* TransactionObserverTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5607EFD21BB8254800605DE3 /* TransactionObserverTests.swift */; };
@@ -301,7 +299,6 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
315013961C21BDA400951DE3 /* FetchedRecordsControllerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = FetchedRecordsControllerTests.swift; path = FetchedRecordsController/FetchedRecordsControllerTests.swift; sourceTree = "<group>"; };
31AEFE3E1C185E63003DB703 /* FetchedRecordsController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = FetchedRecordsController.swift; path = Record/FetchedRecordsController.swift; sourceTree = "<group>"; };
5607EFD21BB8254800605DE3 /* TransactionObserverTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TransactionObserverTests.swift; sourceTree = "<group>"; };
560B3FAB1C19E01000C58EC7 /* DatabasePersistableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabasePersistableTests.swift; sourceTree = "<group>"; };
@@ -483,14 +480,6 @@
name = FetchedRecordsController;
sourceTree = "<group>";
};
315013951C21BD6200951DE3 /* FetchedRecordsController */ = {
isa = PBXGroup;
children = (
315013961C21BDA400951DE3 /* FetchedRecordsControllerTests.swift */,
);
name = FetchedRecordsController;
sourceTree = "<group>";
};
5607EFD11BB8253300605DE3 /* TransactionObserver */ = {
isa = PBXGroup;
children = (
@@ -548,7 +537,6 @@
56CB92EC1BA453E500DFD5FC /* CrashTests.swift */,
56A238111B9C74A90082EB20 /* Core */,
56B7F4281BE149F600E39BBF /* CoreGraphics */,
315013951C21BD6200951DE3 /* FetchedRecordsController */,
56F0B98C1B6001C600A2F135 /* Foundation */,
56A238231B9C74A90082EB20 /* Migrations */,
56A238251B9C74A90082EB20 /* Record */,
@@ -1307,7 +1295,6 @@
565B0FF01BBC7D980098DE03 /* RowConvertibleTests.swift in Sources */,
56E8CE0E1BB4FA5600828BEC /* DatabaseValueConvertibleFetchTests.swift in Sources */,
56A238581B9C74A90082EB20 /* PrimaryKeyRowIDTests.swift in Sources */,
315013981C21BDA400951DE3 /* FetchedRecordsControllerTests.swift in Sources */,
56A238621B9C74A90082EB20 /* RecordEditedTests.swift in Sources */,
56A2385C1B9C74A90082EB20 /* PrimaryKeySingleWithReplaceConflictResolutionTests.swift in Sources */,
56A238401B9C74A90082EB20 /* DatabaseValueConvertibleSubclassTests.swift in Sources */,
@@ -1366,7 +1353,6 @@
56A238611B9C74A90082EB20 /* RecordEditedTests.swift in Sources */,
56A2385B1B9C74A90082EB20 /* PrimaryKeySingleWithReplaceConflictResolutionTests.swift in Sources */,
565B0FEF1BBC7D980098DE03 /* RowConvertibleTests.swift in Sources */,
315013971C21BDA400951DE3 /* FetchedRecordsControllerTests.swift in Sources */,
56A2383F1B9C74A90082EB20 /* DatabaseValueConvertibleSubclassTests.swift in Sources */,
56E8CE0D1BB4FA5600828BEC /* DatabaseValueConvertibleFetchTests.swift in Sources */,
56A2383B1B9C74A90082EB20 /* DatabaseErrorTests.swift in Sources */,
@@ -1401,6 +1387,7 @@
56A2387B1B9C75030082EB20 /* Configuration.swift in Sources */,
56B7F42E1BE14CD400E39BBF /* CGFloat.swift in Sources */,
56A2389B1B9C752B0082EB20 /* DatabaseValueConvertible.swift in Sources */,
317194AF1C2304AA002E3BDA /* FetchedRecordsController.swift in Sources */,
56A2380F1B9C74460082EB20 /* RawRepresentable.swift in Sources */,
56A238AA1B9C7E540082EB20 /* StatementArguments+Foundation.swift in Sources */,
56A238891B9C75030082EB20 /* SelectStatement.swift in Sources */,
@@ -1418,7 +1405,6 @@
56A238B31B9C919D0082EB20 /* NSNumber.swift in Sources */,
56A238B01B9C90BB0082EB20 /* NSString.swift in Sources */,
565D5D681BBC604000DC9BD4 /* Row+Foundation.swift in Sources */,
31AEFE3F1C185E63003DB703 /* FetchedRecordsController.swift in Sources */,
56A238931B9C750B0082EB20 /* DatabaseMigrator.swift in Sources */,
56A2387D1B9C75030082EB20 /* Database.swift in Sources */,
56A238831B9C75030082EB20 /* DatabaseQueue.swift in Sources */,
+23 -25
View File
@@ -6,8 +6,6 @@
// Copyright © 2015 Gwendal Roué. All rights reserved.
//
import UIKit
public class FetchedRecordsController<T: protocol<RowConvertible, DatabaseTableMapping, Hashable>> {
// MARK: - Initialization
@@ -50,7 +48,7 @@ public class FetchedRecordsController<T: protocol<RowConvertible, DatabaseTableM
/// Returns the fetched object at a given indexPath.
public func recordAtIndexPath(indexPath: NSIndexPath) -> T? {
if let record = fetchedRecords?[indexPath.row] {
if let record = fetchedRecords?[indexPath.indexAtPosition(1)] {
return record
} else {
return nil
@@ -81,7 +79,7 @@ public class FetchedRecordsController<T: protocol<RowConvertible, DatabaseTableM
func indexPaths(items: [T]) -> [T:NSIndexPath] {
var indexPathForItem = [T:NSIndexPath]()
for (index, item) in items.enumerate() {
let indexPath = NSIndexPath(forItem: index, inSection: 0)
let indexPath = NSIndexPath(indexes: [0,index], length: 2)
indexPathForItem[item] = indexPath
}
return indexPathForItem
@@ -104,7 +102,7 @@ public class FetchedRecordsController<T: protocol<RowConvertible, DatabaseTableM
fatalError()
}
let update = FetchedRecordsUpdate.Insert(item: item, at: newIndexPath)
let update = FetchedRecordsUpdate.Inserted(item: item, at: newIndexPath)
updates.append(update)
apply(update)
}
@@ -125,25 +123,25 @@ public class FetchedRecordsController<T: protocol<RowConvertible, DatabaseTableM
recordCopy.referenceRow = oldRecord.referenceRow
let changes = recordCopy.persistentChangedValues
if changes.count > 0 {
let update = FetchedRecordsUpdate.Update(item: newItem, at: newIndexPath, changes: changes)
let update = FetchedRecordsUpdate.Updated(item: oldItem, at: newIndexPath, changes: changes)
updates.append(update)
apply(update)
}
} else {
// Not a record
let update = FetchedRecordsUpdate.Update(item: newItem, at: newIndexPath, changes: nil)
let update = FetchedRecordsUpdate.Updated(item: oldItem, at: newIndexPath, changes: nil)
updates.append(update)
apply(update)
}
} else {
// item moved
let update = FetchedRecordsUpdate.Move(item: newItem, from: oldIndexPath, to: newIndexPath)
let update = FetchedRecordsUpdate.Moved(item: newItem, from: oldIndexPath, to: newIndexPath)
updates.append(update)
apply(update)
}
} else {
// item deleted
let update = FetchedRecordsUpdate.Delete(item: oldItem, at: oldIndexPath)
let update = FetchedRecordsUpdate.Deleted(item: oldItem, at: oldIndexPath)
updates.append(update)
apply(update)
}
@@ -198,23 +196,23 @@ public extension FetchedRecordsControllerDelegate {
public enum FetchedRecordsUpdate<T> {
case Insert(item:T, at: NSIndexPath)
case Delete(item:T, at: NSIndexPath)
case Move(item:T, from: NSIndexPath, to: NSIndexPath)
case Update(item:T, at: NSIndexPath, changes: [String: DatabaseValue?]?)
case Inserted(item:T, at: NSIndexPath)
case Deleted(item:T, at: NSIndexPath)
case Moved(item:T, from: NSIndexPath, to: NSIndexPath)
case Updated(item:T, at: NSIndexPath, changes: [String: DatabaseValue?]?)
var description: String {
switch self {
case .Insert(let item, let at):
case .Inserted(let item, let at):
return "Inserted \(item) at indexpath \(at)"
case .Delete(let item, let at):
case .Deleted(let item, let at):
return "Deleted \(item) from indexpath \(at)"
case .Move(let item, let from, let to):
case .Moved(let item, let from, let to):
return "Moved \(item) from indexpath \(from) to indexpath \(to)"
case .Update(let item, let at, let changes):
case .Updated(let item, let at, let changes):
return "Updated \(changes) of \(item) at indexpath \(at)"
}
}
@@ -223,17 +221,17 @@ public enum FetchedRecordsUpdate<T> {
extension Array {
mutating func applyUpdate(update: FetchedRecordsUpdate<Array.Generator.Element>) {
switch update {
case .Insert(let item, let at):
self.insert(item, atIndex: at.item)
case .Inserted(let item, let at):
self.insert(item, atIndex: at.indexAtPosition(1))
case .Delete(_, let from):
self.removeAtIndex(from.item)
case .Deleted(_, let from):
self.removeAtIndex(from.indexAtPosition(1))
case .Move(let item, let from, let to):
self.removeAtIndex(from.item)
self.insert(item, atIndex: to.item)
case .Moved(let item, let from, let to):
self.removeAtIndex(from.indexAtPosition(1))
self.insert(item, atIndex: to.indexAtPosition(1))
case .Update(_, _, _): break
case .Updated(_, _, _): break
}
print(update.description)
}