Skip to content

[v11] Replace Auth worker queue with an actor - checkpoint WIP#13246

Draft
paulb777 wants to merge 7 commits into
mainfrom
pb-auth-actor
Draft

[v11] Replace Auth worker queue with an actor - checkpoint WIP#13246
paulb777 wants to merge 7 commits into
mainfrom
pb-auth-actor

Conversation

@paulb777

@paulb777 paulb777 commented Jul 9, 2024

Copy link
Copy Markdown
Member

WIP checkpoint

forName: UIApplication.didBecomeActiveNotification,
object: nil, queue: nil
) { notification in
self.isAppInBackground = false

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is ported unchanged from the old PR, but why not use applicationState instead of manually tracking the state?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Looks like it would be a good improvement to eliminate self.isAppInBackground, but outside the scope of this PR.

Comment thread FirebaseAuth/Sources/Swift/Auth/AuthWorker.swift Outdated
@google-oss-bot

Copy link
Copy Markdown
Collaborator
1 Warning
⚠️ Did you forget to add a changelog entry? (Add #no-changelog to the PR description to silence this warning.)

Generated by 🚫 Danger

#if canImport(UIKit) // Is a similar mechanism needed on macOS?
applicationDidBecomeActiveObserver =
NotificationCenter.default.addObserver(
forName: UIApplication.didBecomeActiveNotification,

@jesus-mg-ios jesus-mg-ios Jul 16, 2024

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I'm wondering what happen with app extensions, in this way I think UIApplication.didBecomeActiveNotification and didEnterBackgroundNotification would not be fired @paulb777

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good question. This implementation should match the current Objective-C one. Do you see an issue?

@jesus-mg-ios jesus-mg-ios Jul 20, 2024

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Sure, even the didBecomeActiveNotification is available on extensions (imported via UIKit) and the other one as well, it won't be fired. so I would check if it is an extension or not using for example the following code:

var isRunningInExtension: Bool {
    // Check if the bundle path ends with `.appex` (for iOS)
    let bundlePath = Bundle.main.bundlePath
    let isBundlePathExtension = bundlePath.hasSuffix(".appex")
    
    // Check if the `NSExtension` key exists in the `Info.plist` 
    let isNSExtensionPresent = (Bundle.main.infoDictionary?["NSExtension"] as? [String: Any]) != nil
    
    return isBundlePathExtension || isNSExtensionPresent
}

And In case of being true (the output) try to make a check in other way. Dispatch Queues with async after a time don't work on certain extensions as well as timers ... (Because apple timers and dispatch async after tend to sleep when the device is locked or with no activity for a long time) So you can take another approach like going with a counter with the system clock or infinite loop. They are not good approaches but extensions are very restricted. My suggestion in this case is surround with an if the observers and delegate in developers to run any kinda code, calling a func, when activity is registered on their extensions, in this way you won't impact performance in extensions.

Comment thread FirebaseAuth/Sources/Swift/Auth/Auth.swift Outdated
}

/// Configures Firebase Auth to connect to an emulated host instead of the remote backend.
@objc open func useEmulator(withHost host: String, port: Int) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think this code shouldn't be on a production framework cause it won't happen. Is there any way get rid of it without removing the ability to use it whereas developers are coding or testing on pre-production environments?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It could be possible, but I'm not convinced the usability impact would be worth the relatively small size overhead increase.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Sure, maybe an approach like surrounding the function with a comment (one above and one to close it) or any kinda special macro compiler for those who wants to remove it from production could make the trick. Who wants to remove it from prod, could build the frameworks by itself.

@paulb777 paulb777 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@jesus-mg-ios Thanks for taking a look!

#if canImport(UIKit) // Is a similar mechanism needed on macOS?
applicationDidBecomeActiveObserver =
NotificationCenter.default.addObserver(
forName: UIApplication.didBecomeActiveNotification,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good question. This implementation should match the current Objective-C one. Do you see an issue?

Comment thread FirebaseAuth/Sources/Swift/Auth/Auth.swift Outdated
}

/// Configures Firebase Auth to connect to an emulated host instead of the remote backend.
@objc open func useEmulator(withHost host: String, port: Int) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It could be possible, but I'm not convinced the usability impact would be worth the relatively small size overhead increase.

@jesus-mg-ios

Copy link
Copy Markdown

@jesus-mg-ios Thanks for taking a look!

Anytime

@paulb777
paulb777 marked this pull request as draft August 9, 2024 23:16
@paulb777 paulb777 added this to the Firebase 12 milestone Oct 8, 2024
@paulb777

paulb777 commented Oct 8, 2024

Copy link
Copy Markdown
Member Author

Parts of this PR have been implemented in other PRs.

The bulk of moving the auth worker to an actor may need to wait for a breaking change release since public APIs doing dispatch_sync's may need to be converted to async APIs - or other alternatives like part of initialization.

Marking to be considered with Firebase 12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants