-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from Infomaniak/core-db
feat: Split ios-core into Core and Core DB
- Loading branch information
Showing
11 changed files
with
851 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
Sources/InfomaniakCore/SuddenTermination/ExpiringActivity.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
Infomaniak Core - iOS | ||
Copyright (C) 2024 Infomaniak Network SA | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import Foundation | ||
|
||
/// Delegation mechanism to notify the end of an `ExpiringActivity` | ||
public protocol ExpiringActivityDelegate: AnyObject { | ||
/// Called when the system is requiring us to terminate an expiring activity | ||
func backgroundActivityExpiring() | ||
} | ||
|
||
/// Something that can perform arbitrary short background tasks, with a super simple API. | ||
public protocol ExpiringActivityable { | ||
/// Common init method | ||
/// - Parameters: | ||
/// - id: Something to identify the background activity in debug | ||
/// - qos: QoS used by the underlying queues | ||
/// - delegate: The delegate to notify we should terminate | ||
init(id: String, qos: DispatchQoS, delegate: ExpiringActivityDelegate?) | ||
|
||
/// init method | ||
/// - Parameters: | ||
/// - id: Something to identify the background activity in debug | ||
/// - delegate: The delegate to notify we should terminate | ||
init(id: String, delegate: ExpiringActivityDelegate?) | ||
|
||
/// Register with the system an expiring activity | ||
func start() | ||
|
||
/// Terminate all the expiring activities | ||
func endAll() | ||
|
||
/// True if the system asked to stop the background activity | ||
var shouldTerminate: Bool { get } | ||
} | ||
|
||
public final class ExpiringActivity: ExpiringActivityable { | ||
private let qos: DispatchQoS | ||
|
||
private let queue: DispatchQueue | ||
|
||
private let processInfo = ProcessInfo.processInfo | ||
|
||
var locks = [TolerantDispatchGroup]() | ||
|
||
let id: String | ||
|
||
public var shouldTerminate = false | ||
|
||
weak var delegate: ExpiringActivityDelegate? | ||
|
||
// MARK: Lifecycle | ||
|
||
public init(id: String, qos: DispatchQoS, delegate: ExpiringActivityDelegate?) { | ||
self.id = id | ||
self.qos = qos | ||
self.delegate = delegate | ||
queue = DispatchQueue(label: "com.infomaniak.ExpiringActivity.sync", qos: qos) | ||
} | ||
|
||
public convenience init(id: String = "\(#function)-\(UUID().uuidString)", delegate: ExpiringActivityDelegate? = nil) { | ||
self.init(id: id, qos: .userInitiated, delegate: delegate) | ||
} | ||
|
||
deinit { | ||
queue.sync { | ||
assert(locks.isEmpty, "please make sure to call 'endAll()' once explicitly before releasing this object") | ||
} | ||
} | ||
|
||
public func start() { | ||
let group = TolerantDispatchGroup(qos: qos) | ||
|
||
queue.sync { | ||
self.locks.append(group) | ||
} | ||
|
||
#if os(macOS) | ||
// We block a non cooperative queue that matches current QoS | ||
DispatchQueue.global(qos: qos.qosClass).async { | ||
self.processInfo.performActivity(options: .suddenTerminationDisabled, reason: self.id) { | ||
// No expiration handler as we are running on macOS | ||
group.enter() | ||
group.wait() | ||
} | ||
} | ||
#else | ||
// Make sure to not lock an unexpected thread that would deinit() | ||
processInfo.performExpiringActivity(withReason: id) { [weak self] shouldTerminate in | ||
guard let self else { | ||
return | ||
} | ||
|
||
if shouldTerminate { | ||
self.shouldTerminate = true | ||
delegate?.backgroundActivityExpiring() | ||
} | ||
|
||
group.enter() | ||
group.wait() | ||
} | ||
#endif | ||
} | ||
|
||
public func endAll() { | ||
queue.sync { | ||
// Release locks, oldest first | ||
for group in locks.reversed() { | ||
group.leave() | ||
} | ||
locks.removeAll() | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
Infomaniak Core - iOS | ||
Copyright (C) 2024 Infomaniak Network SA | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import Foundation | ||
import RealmSwift | ||
|
||
public extension Realm { | ||
/// Only emits a write signal if not already within a write transaction | ||
func safeWrite(_ block: () throws -> Void) throws { | ||
if isInWriteTransaction { | ||
try block() | ||
} else { | ||
try write(block) | ||
} | ||
} | ||
|
||
/// Shorthand for object from primary key | ||
func getObject<RealmObject: Object, KeyType>(id: KeyType) -> RealmObject? { | ||
object(ofType: RealmObject.self, forPrimaryKey: id) | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...re/Extensions/RealmObject+Extension.swift → ...DB/Extensions/RealmObject+Extension.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
Infomaniak Core - iOS | ||
Copyright (C) 2024 Infomaniak Network SA | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import Foundation | ||
import RealmSwift | ||
|
||
/// Something that can access a realm | ||
/// | ||
/// Do not expose a realm directly in your app, use Transactionable instead | ||
/// | ||
public protocol RealmAccessible { | ||
/// Fetches an up to date realm for a given configuration, or fail in a controlled manner | ||
func getRealm() -> Realm | ||
} | ||
|
||
/// Something that can provide a realm configuration | ||
public protocol RealmConfigurable { | ||
/// Configuration for a given realm | ||
var realmConfiguration: Realm.Configuration { get } | ||
|
||
/// Set `isExcludedFromBackup = true` to the folder where realm is located to exclude a realm cache from an iCloud backup | ||
/// - Important: Avoid calling this method too often as this can be expensive, prefer calling it once at init time | ||
func excludeRealmFromBackup() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
Infomaniak Core - iOS | ||
Copyright (C) 2024 Infomaniak Network SA | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import Foundation | ||
import InfomaniakCore | ||
import RealmSwift | ||
|
||
/// Shared protected DB transaction implementation. | ||
/// | ||
/// Only write transactions are protected from sudden termination, will extend to read if required. | ||
public struct TransactionExecutor: Transactionable { | ||
let realmAccessible: RealmAccessible | ||
|
||
public init(realmAccessible: RealmAccessible) { | ||
self.realmAccessible = realmAccessible | ||
} | ||
|
||
// MARK: Transactionable | ||
|
||
public func fetchObject<Element: Object, KeyType>(ofType type: Element.Type, forPrimaryKey key: KeyType) -> Element? { | ||
autoreleasepool { | ||
let realm = realmAccessible.getRealm() | ||
let object = realm.object(ofType: type, forPrimaryKey: key) | ||
return object | ||
} | ||
} | ||
|
||
public func fetchObject<Element: RealmFetchable>(ofType type: Element.Type, | ||
filtering: (Results<Element>) -> Element?) -> Element? { | ||
autoreleasepool { | ||
let realm = realmAccessible.getRealm() | ||
let objects = realm.objects(type) | ||
let filteredObject = filtering(objects) | ||
return filteredObject | ||
} | ||
} | ||
|
||
public func fetchResults<Element: RealmFetchable>( | ||
ofType type: Element.Type, | ||
filtering: (RealmSwift.Results<Element>) -> RealmSwift.Results<Element> | ||
) -> RealmSwift.Results<Element> { | ||
autoreleasepool { | ||
let realm = realmAccessible.getRealm() | ||
let objects = realm.objects(type) | ||
let filteredCollection = filtering(objects) | ||
return filteredCollection | ||
} | ||
} | ||
|
||
public func writeTransaction(withRealm realmClosure: (Realm) throws -> Void) throws { | ||
try autoreleasepool { | ||
let expiringActivity = ExpiringActivity() | ||
expiringActivity.start() | ||
defer { | ||
expiringActivity.endAll() | ||
} | ||
|
||
let realm = realmAccessible.getRealm() | ||
try realm.safeWrite { | ||
try realmClosure(realm) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.