Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StoreRef for AppGroups #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions DataKernel/Classes/Contracts/Refs/StoreRef.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,42 @@
import Foundation

public enum StoreRef: Equatable {
case named(String)
case url(URL)

case named(String)
case namedInGroup(name: String, appGroup: String)

public func location() -> URL {
switch self {
case .url(let url): return url
case .named(let name): return Foundation.URL(fileURLWithPath: FileUtils.documents()).appendingPathComponent(name)
case let .url(url):
return url

case let .named(name):
return Foundation.URL(fileURLWithPath: FileUtils.documents()).appendingPathComponent(name)

case let .namedInGroup(name, appGroup):
let containerUrl = FileUtils.documents(in: appGroup)
guard let dbDirUrl = containerUrl?.appendingPathComponent(dataKernelPath) else {
assert(false)
return StoreRef.named(name).location()
}
return dbDirUrl.appendingPathComponent(name)
}
}
}

// NOTE: If we move the store from a local directory to a shared one, we'd like to know where to migrate it from
func previousLocation() -> URL? {
switch self {
case let .namedInGroup(name, _):
return StoreRef.named(name).location()

default:
return nil
}
}
}

public func == (lhs: StoreRef, rhs: StoreRef) -> Bool {
return lhs.location() == rhs.location()
}

private let dataKernelPath = "DataKernel"
4 changes: 4 additions & 0 deletions DataKernel/Classes/Helpers/FileUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ class FileUtils {
static func documents() -> String {
return NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
}

static func documents(in appGroup: String) -> URL? {
return FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup)
}
}
17 changes: 16 additions & 1 deletion DataKernelTests/Classes/Helpers/FileUtilsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,20 @@ class FileUtilsTests: XCTestCase {

XCTAssertEqual(path, NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0], "should return the proper documents directory")
}


// NOTE: disabled because for using app groups there should be a capability with entitlements, but for public project it is impossible
// NOTE: you can test such thing in your own project
// func testProperPathInAppGroup() {
// guard let url = FileUtils.documents(in: "group") else {
// XCTAssert(false, "url should be set")
// return
// }
//
// guard let expected = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group") else {
// XCTAssert(false, "expected should be set")
// return
// }
//
// XCTAssertEqual(url, expected, "should return the proper documents directory in app group 'group'")
// }
}