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

Syncing fixes #3488

Merged
merged 2 commits into from
Nov 6, 2024
Merged
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
29 changes: 14 additions & 15 deletions ElementX/Sources/Application/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg

/// Common background task to continue long-running tasks in the background.
private var backgroundTask: UIBackgroundTaskIdentifier?

private var isSuspended = false

private var userSession: UserSessionProtocol? {
didSet {
Expand Down Expand Up @@ -150,6 +148,8 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
switch action {
case .startCall(let roomID):
self?.handleAppRoute(.call(roomID: roomID))
case .receivedIncomingCallRequest:
self?.scheduleDelayedSyncStop()
Copy link
Member

@pixlwave pixlwave Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just discussed that we need to double-check the impact of this if the app is in the foreground.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checked, seems fine

Background Task 23 ("Called by ElementX.debug.dylib, from $s8ElementX14AppCoordinatorC23scheduleDelayedSyncStop33_E85D5AAE1EFD17BA4327A2E0B56B370DLLyyF"), was created over 30 seconds ago. In applications running in the background, this creates a risk of termination. Remember to call UIApplication.endBackgroundTask(_:) for your task in a timely manner to avoid this.

default:
break
}
Expand Down Expand Up @@ -914,6 +914,11 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
private func applicationWillResignActive() {
MXLog.info("Application will resign active")

scheduleDelayedSyncStop()
scheduleBackgroundAppRefresh()
}

private func scheduleDelayedSyncStop() {
guard backgroundTask == nil else {
return
}
Expand All @@ -928,12 +933,6 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
self.backgroundTask = nil
}
}

isSuspended = true

// This does seem to work if scheduled from the background task above
// Schedule it here instead but with an earliest being date of 30 seconds
scheduleBackgroundAppRefresh()
}

@objc
Expand All @@ -944,12 +943,8 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
appMediator.endBackgroundTask(backgroundTask)
self.backgroundTask = nil
}

if isSuspended {
startSync()
}

isSuspended = false

startSync()
}

// MARK: Background app refresh
Expand Down Expand Up @@ -989,7 +984,11 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
scheduleBackgroundAppRefresh()

task.expirationHandler = { [weak self] in
self?.stopSync() // Attempt to stop the sync loop cleanly.
if UIApplication.shared.applicationState != .active {
// Attempt to stop the sync loop cleanly, only if the app not already running
self?.stopSync()
}

MXLog.info("Background app refresh task expired")
task.setTaskCompleted(success: true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,13 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe
// https://stackoverflow.com/a/41230020/730924
update.remoteHandle = .init(type: .generic, value: roomID)

callProvider.reportNewIncomingCall(with: callID.callKitID, update: update) { error in
callProvider.reportNewIncomingCall(with: callID.callKitID, update: update) { [weak self] error in
if let error {
MXLog.error("Failed reporting new incoming call with error: \(error)")
}

self?.actionsSubject.send(.receivedIncomingCallRequest)

completion()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Combine

enum ElementCallServiceAction {
case receivedIncomingCallRequest
case startCall(roomID: String)
case endCall(roomID: String)
case setAudioEnabled(_ enabled: Bool, roomID: String)
Expand Down
Loading