Skip to content

Commit

Permalink
Don't override designated initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
bfahey committed Oct 25, 2024
1 parent 0cae0c6 commit 5e0420f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 39 deletions.
30 changes: 12 additions & 18 deletions swift-sdk/Internal/IterableCoreDataPersistence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ enum PersistenceConst {
}

let sharedManagedObjectModel: NSManagedObjectModel? = {
guard let url = PersistentContainer.dataModelUrl(fromBundles: [Bundle.main, Bundle(for: PersistentContainer.self)]) else {
let firstBundleURL: URL? = [Bundle.main, Bundle(for: PersistentContainer.self)].lazy.compactMap { bundle in
ResourceHelper.url(
forResource: PersistenceConst.dataModelFileName,
withExtension: PersistenceConst.dataModelExtension,
fromBundle: bundle
)
}.first

guard let url = firstBundleURL else {
ITBError("Could not find \(PersistenceConst.dataModelFileName).\(PersistenceConst.dataModelExtension) in bundle")
return nil
}
Expand All @@ -39,30 +47,16 @@ final class PersistentContainer: NSPersistentContainer, @unchecked Sendable {
return backgroundContext
}

override init(
name: String = PersistenceConst.dataModelFileName,
managedObjectModel: NSManagedObjectModel? = sharedManagedObjectModel
) {
if let managedObjectModel {
init() {
let name = PersistenceConst.dataModelFileName
if let managedObjectModel = sharedManagedObjectModel {
super.init(name: name, managedObjectModel: managedObjectModel)
} else {
super.init(name: name)
}
viewContext.automaticallyMergesChangesFromParent = true
viewContext.mergePolicy = NSMergePolicy(merge: NSMergePolicyType.mergeByPropertyStoreTrumpMergePolicyType)
}

static func dataModelUrl(fromBundles bundles: [Bundle]) -> URL? {
bundles.lazy.compactMap(dataModelUrl(fromBundle:)).first
}

private static func dataModelUrl(fromBundle bundle: Bundle) -> URL? {
ResourceHelper.url(
forResource: PersistenceConst.dataModelFileName,
withExtension: PersistenceConst.dataModelExtension,
fromBundle: bundle
)
}
}

final class CoreDataPersistenceContextProvider: IterablePersistenceContextProvider {
Expand Down
45 changes: 24 additions & 21 deletions tests/endpoint-tests/OfflineModeE2ETests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,42 +81,45 @@ class OfflineModeEndpointTests: XCTestCase {
"messageId": "msg_1",
],
]
api.trackPushOpen(pushPayload,
dataFields: ["data_field1": "value1"],
onSuccess: { _ in
expectation1.fulfill()
}) { reason, _ in
api.trackPushOpen(
pushPayload,
dataFields: ["data_field1": "value1"],
onSuccess: { _ in
expectation1.fulfill()
}
) { reason, _ in
XCTFail(reason ?? "failed")
}

wait(for: [expectation1], timeout: 15)
}

func test04TrackEvent() throws {
let expectation1 = expectation(description: #function)
let localStorage = MockLocalStorage()
localStorage.offlineMode = true
let api = InternalIterableAPI.initializeForE2E(apiKey: Self.apiKey,
localStorage: localStorage)
let api = InternalIterableAPI.initializeForE2E(
apiKey: Self.apiKey,
localStorage: localStorage
)
api.email = "[email protected]"

api.track("event1",
dataFields: ["data_field1": "value1"],
onSuccess: { _ in
expectation1.fulfill()
}) { reason, _ in

api.track(
"event1",
dataFields: ["data_field1": "value1"],
onSuccess: { _ in
expectation1.fulfill()
}
) { reason, _ in
XCTFail(reason ?? "failed")
}

wait(for: [expectation1], timeout: 15)
}

private static let apiKey = Environment.apiKey!
private static let pushCampaignId = Environment.pushCampaignId!
private static let pushTemplateId = Environment.pushTemplateId!
private static let inAppCampaignId = Environment.inAppCampaignId!
private lazy var persistenceContextProvider: IterablePersistenceContextProvider = {
let provider = CoreDataPersistenceContextProvider()!
return provider
}()
private lazy var persistenceContextProvider: IterablePersistenceContextProvider = CoreDataPersistenceContextProvider()
}

0 comments on commit 5e0420f

Please sign in to comment.