Skip to content

Commit

Permalink
Integrate with newest Contentful SDK and add option to set limit for …
Browse files Browse the repository at this point in the history
…sync (#103)
  • Loading branch information
tomkowz authored Dec 3, 2020
1 parent 632df27 commit 0c7e879
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CONTENTFUL_PERSISTENCE_VERSION=0.16.1
CONTENTFUL_PERSISTENCE_VERSION=0.17.0
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github "contentful/contentful.swift" ~> 5.3.1
github "contentful/contentful.swift" ~> 5.4.0

4 changes: 2 additions & 2 deletions Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github "AliSoftware/OHHTTPStubs" "9.0.0"
github "contentful/contentful.swift" "5.3.1"
github "AliSoftware/OHHTTPStubs" "9.1.0"
github "contentful/contentful.swift" "5.4.0"
2 changes: 1 addition & 1 deletion Carthage/Checkouts/OHHTTPStubs
Submodule OHHTTPStubs updated 23 files
+6 −0 CHANGELOG.md
+9 −9 Examples/ObjC/Podfile.lock
+2 −2 Examples/ObjC/Pods/Local Podspecs/OHHTTPStubs.podspec.json
+9 −9 Examples/ObjC/Pods/Manifest.lock
+168 −165 Examples/ObjC/Pods/Pods.xcodeproj/project.pbxproj
+10 −0 Examples/ObjC/Pods/Target Support Files/OHHTTPStubs/OHHTTPStubs.debug.xcconfig
+10 −0 Examples/ObjC/Pods/Target Support Files/OHHTTPStubs/OHHTTPStubs.release.xcconfig
+1 −0 Examples/ObjC/Pods/Target Support Files/Pods-OHHTTPStubsDemo/Pods-OHHTTPStubsDemo.debug.xcconfig
+1 −0 Examples/ObjC/Pods/Target Support Files/Pods-OHHTTPStubsDemo/Pods-OHHTTPStubsDemo.release.xcconfig
+10 −10 Examples/Swift/Podfile.lock
+2 −2 Examples/Swift/Pods/Local Podspecs/OHHTTPStubs.podspec.json
+10 −10 Examples/Swift/Pods/Manifest.lock
+220 −217 Examples/Swift/Pods/Pods.xcodeproj/project.pbxproj
+1 −1 Examples/Swift/Pods/Target Support Files/OHHTTPStubs/OHHTTPStubs-Info.plist
+11 −0 Examples/Swift/Pods/Target Support Files/OHHTTPStubs/OHHTTPStubs.debug.xcconfig
+11 −0 Examples/Swift/Pods/Target Support Files/OHHTTPStubs/OHHTTPStubs.release.xcconfig
+46 −10 Examples/Swift/Pods/Target Support Files/Pods-OHHTTPStubsDemo/Pods-OHHTTPStubsDemo-frameworks.sh
+1 −0 Examples/Swift/Pods/Target Support Files/Pods-OHHTTPStubsDemo/Pods-OHHTTPStubsDemo.debug.xcconfig
+1 −0 Examples/Swift/Pods/Target Support Files/Pods-OHHTTPStubsDemo/Pods-OHHTTPStubsDemo.release.xcconfig
+1 −1 OHHTTPStubs.podspec
+1 −1 [email protected]
+51 −0 Sources/OHHTTPStubsSwift/OHHTTPStubsSwift.swift
+47 −0 Tests/OHHTTPStubsSwiftTests/SwiftHelpersTests.swift
2 changes: 1 addition & 1 deletion Config.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CONTENTFUL_PERSISTENCE_VERSION=0.16.1
CONTENTFUL_PERSISTENCE_VERSION=0.17.0
2 changes: 1 addition & 1 deletion Scripts/.env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CONTENTFUL_PERSISTENCE_VERSION=0.16.1
CONTENTFUL_PERSISTENCE_VERSION=0.17.0
2 changes: 1 addition & 1 deletion Scripts/Config.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CONTENTFUL_PERSISTENCE_VERSION=0.16.1
CONTENTFUL_PERSISTENCE_VERSION=0.17.0
9 changes: 9 additions & 0 deletions Sources/ContentfulPersistence/CoreDataStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ public class CoreDataStore: PersistenceStore {
return items
}

public func fetchOne<T>(
type: Any.Type,
predicate: NSPredicate
) throws -> T {
let request = try fetchRequest(for: type, predicate: predicate)
request.fetchLimit = 1
return try context.fetch(request).first as! T
}

/**
Returns an array of names of properties the given type stores persistently.

Expand Down
12 changes: 12 additions & 0 deletions Sources/ContentfulPersistence/PersistenceStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ public protocol PersistenceStore {
*/
func fetchAll<T>(type: Any.Type, predicate: NSPredicate) throws -> [T]

/**
Fetches one object of a specific type which matches the predicate.

- parameter type: Type of which object should be fetched.
- parameter predicate: The predicate used for matching object to fetch.

- throws: If an invalid type was specified

- returns: Matching object
*/
func fetchOne<T>(type: Any.Type, predicate: NSPredicate) throws -> T

/**
Returns an array of names of properties the given type stores persistently.

Expand Down
20 changes: 11 additions & 9 deletions Sources/ContentfulPersistence/SynchronizationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,26 @@ public class SynchronizationManager: PersistenceIntegration {
method is thread safe and will delegate to the thread that your data store is tied to.

Execute queries on your local data store in the callback for this method.

- parameter limit: Number of elements per page. See documentation for details.
*/
public func sync(then completion: @escaping ResultsHandler<SyncSpace>) {
public func sync(limit: Int? = nil, then completion: @escaping ResultsHandler<SyncSpace>) {
resolveCachedRelationships { [weak self] in
self?.syncSafely(then: completion)
self?.syncSafely(limit: limit, then: completion)
}
}

private func syncSafely(then completion: @escaping ResultsHandler<SyncSpace>) {
private func syncSafely(limit: Int?, then completion: @escaping ResultsHandler<SyncSpace>) {
let safeCompletion: ResultsHandler<SyncSpace> = { [weak self] result in
self?.persistentStore.performBlock {
completion(result)
}
}

if let syncToken = self.syncToken {
client?.sync(for: SyncSpace(syncToken: syncToken), then: safeCompletion)
client?.sync(for: SyncSpace(syncToken: syncToken, limit: limit), then: safeCompletion)
} else {
client?.sync(for: SyncSpace(), then: safeCompletion)
client?.sync(for: SyncSpace(limit: limit), then: safeCompletion)
}
}

Expand Down Expand Up @@ -331,13 +333,13 @@ public class SynchronizationManager: PersistenceIntegration {
let type = persistenceModel.assetType

let fetchPredicate = predicate(for: asset.id, localeCodes: localeCodes)
let fetched: [AssetPersistable]? = try? persistentStore.fetchAll(type: type, predicate: fetchPredicate)
let fetched: AssetPersistable? = try? persistentStore.fetchOne(type: type, predicate: fetchPredicate)

for localeCode in localeCodes {
asset.setLocale(withCode: localeCode)

let persistable: AssetPersistable
if let fetched = (fetched?.first { $0.localeCode == localeCode }) {
if let fetched = fetched, fetched.localeCode == localeCode {
persistable = fetched
} else {
do {
Expand Down Expand Up @@ -395,13 +397,13 @@ public class SynchronizationManager: PersistenceIntegration {
guard let type = persistenceModel.entryTypes.filter({ $0.contentTypeId == contentTypeId }).first else { return }

let fetchPredicate = predicate(for: entry.id, localeCodes: localeCodes)
let fetched: [EntryPersistable]? = try? persistentStore.fetchAll(type: type, predicate: fetchPredicate)
let fetched: EntryPersistable? = try? persistentStore.fetchOne(type: type, predicate: fetchPredicate)

for localeCode in localeCodes {
entry.setLocale(withCode: localeCode)
let persistable: EntryPersistable

if let fetched = (fetched?.first { $0.localeCode == localeCode }) {
if let fetched = fetched, fetched.localeCode == localeCode {
persistable = fetched
} else {
do {
Expand Down
10 changes: 10 additions & 0 deletions Tests/ContentfulPersistenceTests/SynchronizationManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ class ContentfulPersistenceTests: XCTestCase {
waitForExpectations(timeout: 10.0, handler: nil)
}

func testFetchOneAssetFromTheStore() {
let expectation = self.expectation(description: "Can fetch single asset from the store.")
self.client.sync { _ in
let asset: Asset? = try? self.store.fetchOne(type: Asset.self, predicate: self.assetPredicate)
XCTAssertEqual(asset?.id, "bXvdSYHB3Guy2uUmuEco8")
expectation.fulfill()
}
waitForExpectations(timeout: 10.0, handler: nil)
}

func testCanStoreAssetPersistables() {
let expectation = self.expectation(description: "Can store Asset Persistables expecatation")

Expand Down

0 comments on commit 0c7e879

Please sign in to comment.