swift - performSegue not working in RxSwift Observable subscribe - ios

I have the following code:
loginViewModel.facebookLogin
.asObservable()
subscribe() { [unowned self] facebookLogin in
if let isLoggedIn = facebookLogin.element?.isLoggedIn {
if isLoggedIn {
elf.performSegue(withIdentifier: "toRestaurantSelect", sender: self)
}
}
if let didLoginFail = facebookLogin.element?.didLoginFail {
self.errorLabel.isHidden = !didLoginFail
}
}
.disposed(by: disposeBag)
The facebookLogin is a Variable and is updated when the user logs in. However, the performSegue is not called (the condition is true). Strangely enough, if I turn on Slow animations in the emulator the segue is executed. When Slow animations are turned off the segue doesn't execute (the Facebook login works). Any help is appreciated. Thanks!

i think you should use the main thread to make it work
loginViewModel.facebookLogin
.asObservable()
.subscribe() { [unowned self] facebookLogin in
if let isLoggedIn = facebookLogin.element?.isLoggedIn {
if isLoggedIn {
DispatchQueue.main.async {
self.performSegue(withIdentifier: "toRestaurantSelect", sender: self)
}
}
}
if let didLoginFail = facebookLogin.element?.didLoginFail {
self.errorLabel.isHidden = !didLoginFail
}
}
.disposed(by: disposeBag)

Do the observation with the main scheduler:
loginViewModel.facebookLogin
.asObservable()
// Switch to the main scheduler
.observeOn(MainScheduler.instance)
subscribe() { [unowned self] facebookLogin in
if let isLoggedIn = facebookLogin.element?.isLoggedIn {
if isLoggedIn {
elf.performSegue(withIdentifier: "toRestaurantSelect", sender: self)
}
}
if let didLoginFail = facebookLogin.element?.didLoginFail {
self.errorLabel.isHidden = !didLoginFail
}
}
.disposed(by: disposeBag)

Related

RxSwift Subscriber receiving multiple events

Consider the below code.
On tapButton, we subscribe to an Observable isFetched and then call fetchPopularMovies().
fetchPopularMovies() in turn calls an API. When the response is received, we will send OnNext(true) event.
Problem is, I receive multiple events on 2nd button tap onwards. If I add onCompleted(), I don't even receive events on 2nd button tap onwards. My expectation is that one event will be triggered on each button tap. What am I missing here?
class ViewController: UIViewController {
let popularMoviesURL = URL(string: "https://api.themoviedb.org/3/movie/popular?api_key=API_KEY")
var isFetched = BehaviorSubject<Bool?>(value:nil)
let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func tapButton(_ sender: Any) {
let observable = isFetched.asObservable()
observable.subscribe(onNext: { observer in
guard let result = observer else { return }
print(result)
print("onNext Recieved")
}, onError: { _ in
print("onError Recieved")
}).disposed(by: disposeBag)
fetchPopularMovies()
}
func fetchPopularMovies() {
let task = URLSession.shared.dataTask(with: popularMoviesURL!) {(data, response, error) in
guard let _ = data else { return }
self.isFetched.onNext(true)
//self.isFetched.onCompleted()
}
task.resume()
}
}
Reactive code is declarative. It's "setup" code. So it should be placed where the comment says "Do any additional setup after loading the view."
The simplest change you can do to fix the problem you are having is to move the subscription into the viewDidLoad method as Satish Patel referenced in his comment.
override func viewDidLoad() {
super.viewDidLoad()
isFetched.subscribe(onNext: { observer in
guard let result = observer else { return }
print(result)
print("onNext Recieved")
}, onError: { _ in
print("onError Recieved")
}).disposed(by: disposeBag)
}
#IBAction func tapButton(_ sender: Any) {
fetchPopularMovies()
}
(Note that Subjects should always be held with lets never vars.)
If you use RxCocoa, you can simplify this code even more:
class ViewController: UIViewController {
let button = UIButton()
let isFetched = BehaviorSubject<Bool?>(value:nil)
let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
let popularMoviesURL = URL(string: "https://api.themoviedb.org/3/movie/popular?api_key=API_KEY")!
let fetchedData = button.rx.tap
.flatMapLatest {
URLSession.shared.rx.data(request: URLRequest(url: popularMoviesURL))
.catch { error in
print("onError Recieved")
return Observable.empty()
}
}
fetchedData
.map { _ in true }
.bind(to: isFetched)
.disposed(by: disposeBag)
}
}
Now, all of your code is "setup code" so it all goes in viewDidLoad.

want to set a self var using guard statement

I have this code, where I'm trying to set a self variable (self?.users) from a view model call. The code snippet looks like this.
override func viewWillAppear(_ animated: Bool) {
DispatchQueue.global().async { [weak self] in
self?.model?.findAll() { [weak self] users, exception in // network call
guard users != nil, self?.users = users else { // Optional type ()? cannot be used as a boolean; test for !=nil instead
}
}
DispatchQueue.main.async {
self?.tableView.reloadData()
}
}
}
I'm capturing [weak self] twice, is that okay?, can I capture it once as weak in the enclosing closure?
Should I use this instead of guard statement?
self?.model?.findAll() { [weak self] users, exception in
if exception != nil {
self?.users = users
}
}
DispatchQueue closures don't cause retain cycles so capture lists are not necessary.
Something like this, to avoid confusion I'd recommend to rename the incoming users and the code to reload the table view must be inside the closure
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
DispatchQueue.global().async {
self.model?.findAll() { [weak self] foundUsers, exception in // network call
guard let foundUsers = foundUsers else { return }
self?.users = foundUsers
DispatchQueue.main.async {
self?.tableView.reloadData()
}
}
}
}
And don’t forget to call super

UIPrinterPickerController not showing on iOS 13

I've been trying to get a UIPrinterPicker to show up but for some reason it just never does. The completion handler gets called immediately. The UIPrintInteractionController shows up just fine but this one refuses to show for some reason. This is the code I am using currently
let picker = UIPrinterPickerController(initiallySelectedPrinter: nil)
picker.present(animated: true) { (controller, complete, error) in
print("done")
}
Implement UIPrinterPickerControllerDelegate so it works for iOS 13
https://developer.apple.com/documentation/uikit/uiprinterpickercontroller/1620514-present
class ViewController: UIViewController {
#IBAction func btnTapped(_ sender: Any) {
let picker = UIPrinterPickerController(initiallySelectedPrinter: nil)
picker.delegate = self
picker.present(animated: true) { (controller, complete, error) in
print("done")
}
}
}
// MARK:- UIPrinterPickerControllerDelegate
extension ViewController: UIPrinterPickerControllerDelegate {
func printerPickerControllerParentViewController(_ printerPickerController: UIPrinterPickerController) -> UIViewController? {
return self
}
}

DispatchQueue.main.asyncAfter with on/off switch

I whipped up the below struct as a way to alert the user when there's a slow network connection.
When a function is going to make a call to the server, it creates a ResponseTimer. This sets a delayed notification, which only fires if the responseTimer var isOn = true. When my function get's a response back from the server, set responseTimer.isOn = false.
Here's the struct:
struct ResponseTimer {
var isOn: Bool
init() {
self.isOn = true
self.setDelayedAlert()
}
func setDelayedAlert() {
let timer = DispatchTime.now() + 8
DispatchQueue.main.asyncAfter(deadline: timer) {
if self.isOn {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: toastErrorNotificationKey), object: self, userInfo: ["toastErrorCase" : ToastErrorCase.poorConnection])
}
}
}
And here's how I'd use it
func getSomethingFromFirebase() {
var responseTimer = ResponseTimer()
ref.observeSingleEvent(of: .value, with: { snapshot in
responseTimer.isOn = false
//do other stuff
})
}
Even in cases where the response comes back before the 8 second delay completes, the notification is still fired. What am I doing wrong here??? Is there a better pattern to use for something like this?
Thanks for the help!
A better way is to use DispatchSourceTimer which can be cancelled
var timer : DispatchSourceTimer?
func startTimer()
{
if timer == nil {
timer = DispatchSource.makeTimerSource(queue: DispatchQueue.global())
timer!.schedule(deadline: .now() + .seconds(8))
timer!.setEventHandler {
DispatchQueue.main.async {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: toastErrorNotificationKey), object: self, userInfo: ["toastErrorCase" : ToastErrorCase.poorConnection])
}
self.timer = nil
}
timer!.resume()
}
}
func getSomethingFromFirebase() {
startTimer()
ref.observeSingleEvent(of: .value, with: { snapshot in
self.timer?.cancel()
self.timer = nil
//do other stuff
})
}
There are a few approaches:
invalidate a Timer in deinit
Its implementation might look like:
class ResponseTimer {
private weak var timer: Timer?
func schedule() {
timer = Timer.scheduledTimer(withTimeInterval: 8, repeats: false) { _ in // if you reference `self` in this block, make sure to include `[weak self]` capture list, too
// do something
}
}
func invalidate() {
timer?.invalidate()
}
// you might want to make sure you `invalidate` this when it’s deallocated just in
// case you accidentally had path of execution that failed to call `invalidate`.
deinit {
invalidate()
}
}
And then you can do:
var responseTimer: ResponseTimer?
func getSomethingFromFirebase() {
responseTimer = ResponseTimer()
responseTimer.schedule()
ref.observeSingleEvent(of: .value) { snapshot in
responseTimer?.invalidate()
//do other stuff
}
}
Use asyncAfter with DispatchWorkItem, which you can cancel:
class ResponseTimer {
private var item: DispatchWorkItem?
func schedule() {
item = DispatchWorkItem { // [weak self] in // if you reference `self` in this block, uncomment this `[weak self]` capture list, too
// do something
}
DispatchQueue.main.asyncAfter(deadline: .now() + 5, execute: item!)
}
func invalidate() {
item?.cancel()
item = nil
}
deinit {
invalidate()
}
}
Use DispatchTimerSource, which cancels automatically when it falls out of scope:
struct ResponseTimer {
private var timer: DispatchSourceTimer?
mutating func schedule() {
timer = DispatchSource.makeTimerSource(queue: .main)
timer?.setEventHandler { // [weak self] in // if you reference `self` in the closure, uncomment this
NotificationCenter.default.post(name: notification, object: nil)
}
timer?.schedule(deadline: .now() + 8)
timer?.activate()
}
mutating func invalidate() {
timer = nil
}
}
In all three patterns, the timer will be canceled when ResponseTimer falls out of scope.

is it possible to record part of screen with Replaykit?

Here is the start and stop functions.
#IBAction func startRecordingAction(sender: AnyObject) {
activityView.hidden = false
// start recording
recorder.startRecordingWithMicrophoneEnabled(true) { [unowned self] (error) in
dispatch_async(dispatch_get_main_queue()) {
[unowned self] in
self.activityView.hidden = true
}
if let error = error {
print("Failed start recording: \(error.localizedDescription)")
return
}
print("Start recording")
self.buttonEnabledControl(true)
}
}
#IBAction func stopRecordingAction(sender: AnyObject) {
activityView.hidden = false
//end recording
recorder.stopRecordingWithHandler({ [unowned self] (previewViewController, error) in
dispatch_async(dispatch_get_main_queue()) {
self.activityView.hidden = true
}
self.buttonEnabledControl(false)
if let error = error {
print("Failed stop recording: \(error.localizedDescription)")
return
}
print("Stop recording")
previewViewController?.previewControllerDelegate = self
dispatch_async(dispatch_get_main_queue()) { [unowned self] in
// show preview vindow
self.presentViewController(previewViewController!, animated: true, completion: nil)
}
})
}
func screenRecorderDidChangeAvailability(screenRecorder: RPScreenRecorder) {
let availability = screenRecorder.available
print("Availability: \(availability)\n");
}
// MARK: - RPPreviewViewControllerDelegate
// called when preview is finished
func previewControllerDidFinish(previewController: RPPreviewViewController) {
print("Preview finish");
dispatch_async(dispatch_get_main_queue()) {
[unowned previewController] in
// close preview window
previewController.dismissViewControllerAnimated(true, completion: nil)
}
}
I want to record only a part of screen and I want to show a custom alert, not ReplayKit standard alert message. I can use 3 party pods, no problem.
Maybe you can advice me a different way, without ReplayKit.
Unfortunately, you cannot record a particular UIView for now with/Using Replay Kit.
For recording particular view here are some alternative's hope this helps you out.
https://github.com/wess/Glimpse
https://github.com/adam-roth/screen-cap-view
https://github.com/andydrizen/UIViewRecorder
Hope this helps you out.

Resources