Skip to content

Commit

Permalink
Try to optimize Session+RemoteChanges & Sesion+UpdateUser
Browse files Browse the repository at this point in the history
  • Loading branch information
sinoru committed May 18, 2022
1 parent 2baafcd commit eb38479
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 39 deletions.
42 changes: 17 additions & 25 deletions TweetNestKit/Session/Session+RemoteChanges.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,34 +85,28 @@ extension Session {
}

private func handlePersistentHistoryTransactions(_ persistentHistoryTransactions: [NSPersistentHistoryTransaction]) async {
do {
try await withExtendedBackgroundExecution {
await withTaskGroup(of: Void.self) { taskGroup in
taskGroup.addTask {
await self.updateNotifications(transactions: persistentHistoryTransactions)
}

taskGroup.addTask {
await self.updateAccountCredential(transactions: persistentHistoryTransactions)
}
await withTaskGroup(of: Void.self) { taskGroup in
taskGroup.addTask {
await self.updateNotifications(transactions: persistentHistoryTransactions)
}

taskGroup.addTask(priority: .utility) {
await self.cleansingAccount(transactions: persistentHistoryTransactions)
}
taskGroup.addTask {
await self.updateAccountCredential(transactions: persistentHistoryTransactions)
}

taskGroup.addTask(priority: .utility) {
await self.cleansingUser(transactions: persistentHistoryTransactions)
}
taskGroup.addTask(priority: .utility) {
await self.cleansingAccount(transactions: persistentHistoryTransactions)
}

taskGroup.addTask(priority: .utility) {
await self.cleansingUserDataAssets(transactions: persistentHistoryTransactions)
}
taskGroup.addTask(priority: .utility) {
await self.cleansingUser(transactions: persistentHistoryTransactions)
}

await taskGroup.waitForAll()
}
taskGroup.addTask(priority: .utility) {
await self.cleansingUserDataAssets(transactions: persistentHistoryTransactions)
}
} catch {
self.logger.error("Error occurred while handle persistent history transactions: \(error as NSError, privacy: .public)")

await taskGroup.waitForAll()
}
}

Expand Down Expand Up @@ -386,8 +380,6 @@ extension Session {

let needsToBeRemovedObjectIDs = changes.keys.subtracting(hasChangesObjectIDs)
Task.detached(priority: .utility) {
await Task.yield()

do {
try withExtendedBackgroundExecution(expirationHandler: nil) {
let cloudKitRecordIDs = self.persistentContainer.recordIDs(for: needsToBeRemovedObjectIDs.elements)
Expand Down
53 changes: 39 additions & 14 deletions TweetNestKit/Session/Session+UpdateUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,29 +83,54 @@ extension Session {

let twitterSession = try await self.twitterSession(for: accountObjectID)

let (followingUserIDs, followerIDs, myBlockingUserIDs, myMutingUserIDs): ([Twitter.User.ID], [Twitter.User.ID], [Twitter.User.ID]?, [Twitter.User.ID]?) = try await withExtendedBackgroundExecution {
async let followingUserIDs = try await Twitter.User.followingUserIDs(forUserID: accountUserID, session: twitterSession).userIDs
async let followerIDs = try await Twitter.User.followerIDs(forUserID: accountUserID, session: twitterSession).userIDs
async let myBlockingUserIDs = accountPreferences.fetchBlockingUsers ? try await Twitter.User.myBlockingUserIDs(session: twitterSession).userIDs : nil
async let myMutingUserIDs = accountPreferences.fetchMutingUsers ? try await Twitter.User.myMutingUserIDs(session: twitterSession).userIDs : nil
async let followingUserIDs = try await withExtendedBackgroundExecution {
try await Twitter.User.followingUserIDs(forUserID: accountUserID, session: twitterSession).userIDs
}

async let followerIDs = try await withExtendedBackgroundExecution {
try await Twitter.User.followerIDs(forUserID: accountUserID, session: twitterSession).userIDs
}

return try await (followingUserIDs, followerIDs, myBlockingUserIDs, myMutingUserIDs)
async let myBlockingUserIDs = try await withExtendedBackgroundExecution {
accountPreferences.fetchBlockingUsers ? try await Twitter.User.myBlockingUserIDs(session: twitterSession).userIDs : nil
}

return try await self.updateUsers(
ids: Set<Twitter.User.ID>([accountUserID] + [followingUserIDs, followerIDs, myBlockingUserIDs, myMutingUserIDs].compacted().joined()),
async let myMutingUserIDs = try await withExtendedBackgroundExecution {
accountPreferences.fetchMutingUsers ? try await Twitter.User.myMutingUserIDs(session: twitterSession).userIDs : nil
}

let additionalAccountUserInfo = try await AdditionalUserInfo(
followingUserIDs: followingUserIDs,
followerIDs: followerIDs,
blockingUserIDs: myBlockingUserIDs,
mutingUserIDs: myMutingUserIDs
)

async let accountUserDetailChanges = self.updateUsers(
ids: [accountUserID],
accountUserID: accountUserID,
twitterSession: twitterSession,
addtionalUserInfos: [
accountUserID: AdditionalUserInfo(
followingUserIDs: followingUserIDs,
followerIDs: followerIDs,
blockingUserIDs: myBlockingUserIDs,
mutingUserIDs: myMutingUserIDs
)
accountUserID: additionalAccountUserInfo
],
context: context
)[accountUserID]

_ = try await self.updateUsers(
ids: Set<Twitter.User.ID>(
[
additionalAccountUserInfo.followingUserIDs,
additionalAccountUserInfo.followerIDs,
additionalAccountUserInfo.blockingUserIDs,
additionalAccountUserInfo.mutingUserIDs
].compacted().joined()
),
accountUserID: accountUserID,
twitterSession: twitterSession,
context: context
)

return try await accountUserDetailChanges
}
}

Expand Down

0 comments on commit eb38479

Please sign in to comment.