Why is Spotify iOS SDK hanging indefinitely on SPTAudioStreamingController.login()? - ios

Following is the code snippet that is hanging:
DispatchQueue.main.async {
log.info("User is a premium user. Logging into player.")
self.player?.login(withAccessToken: self.session!.accessToken)
log.info("Logged into player.")
}
A few notes:
The user is logged in with a premium user account, and a valid oauth token.
In a previous codebase, this worked.
It's running on main.async because in a previous codebase where this worked, I was running on that DispatchQueue.
Running it on the same thread, without the async {} doesn't work either.
Creating a new DispatchQueue, running it on the queue as async, also doesn't work.
According to the debugger, it's hanging on the subcall SPTAudioStreamingController.dispatchToAudioThread.
Any help, or even just long-shot ideas on what could be causing this would be greatly appreciated.

I believe you are missing a call before login that is important. You need to first call the start(withClientId: ) method.
do {
try self.player?.start(withClientId: clientID)
} catch {
log("Failed to start with clientId")
}
In the documentation, calling the start method:
"Start the SPAudioStreamingController thread with the default
audioController."
Which explains why it would hang on the dispatchToAudioThread

Related

GoogleSignIn (iOS) getTokensWithHandler doesn't call its closure after token has expired

I am using Google Sign-In 5.0.2 with a Swift 4 iOS app. Here is my code to get a current id Token:
public static func getJwtToken(completion: #escaping (Result<String, Error>) -> Void) {
assert(Config.isAuthEnabled())
GIDSignIn.sharedInstance()?.currentUser?.authentication.getTokensWithHandler({ gidAuth, error in
if let error = error {
completion(.failure(error))
} else if let token = gidAuth?.idToken {
completion(.success(token))
} else {
assertionFailure("shouldn't have come here")
}
})
}
This works fine when I launch the app.
But if I leave the app running for an hour (to let the id token expire), then the next time getTokensWithHandler is called, it will not call my closure. Subsequent calls to getTokensWithHandler will once again call my closure.
I would like it to behave consistently so that my closure is always called, even if the token needs to be refreshed.
Anyone have any ideas on what I need to do to achieve this?
I figured out the answer to this problem after spending some more time on it.
My problem was I was trying to hack the Google SDK's closure to be synchronous instead of just letting it call the closure when it was ready. The function call that I claimed never called its closure would've called it if I had let the program flow naturally instead of trying to make it call the closure when I expected it to be called. I was using a semaphore somewhere in the call stack that was waiting until the closure got called and I just needed stop making assumptions about when Google's SDK would perform its work.
I'm still trying to get used to this async-first paradigm of Swift.

AWS Transcribe stuck in getTranscriptionJob in iOS

I'm trying to use AWS Transcribe in an iOS app using the aws-sdk-ios. The app starts a transcription job and I can see the job on the AWS console. But the app can't list the jobs, or get a specific job, because it gets stuck in the request to getTranscriptionJob or listTranscriptionJobs, as these requests never complete (I added a print statement and a breakpoint inside the completion block, and it never prints, nor reaches the breakpoint).
I uploaded to GitHub a sample single-view app demonstrating the problem. You'll need an AWS account or IAM user with full permissions on S3 and Transcribe. Insert that account's keys and S3 bucket in ViewController.swift in the appropriate variables.
https://github.com/joaomarceloods/AWSTranscribeBug
I need help. Is this a bug, or am I doing something wrong?
Swift, iOS 13.2, CocoaPods, AWSCore 2.12.1, AWSTranscribe 2.12.1
Most important snippet:
/// `getTranscriptionJob` repeatedly until the status is no longer `inProgress`.
/// However, `getTranscriptionJob` never completes.
var transcriptionInProgress = true
while transcriptionInProgress {
print("getTranscriptionJob")
transcribe.getTranscriptionJob(request).continueWith { task -> Any? in
print("getTranscriptionJob never completes...")
let transcriptionJob = task.result?.transcriptionJob
transcriptionInProgress = transcriptionJob?.transcriptionJobStatus == .inProgress
return nil
}.waitUntilFinished()
}
print("...after the getTranscriptionJob")
I found a solution.
I still don't understand why getTranscriptionJob freezes, but it will execute normally if you run it on DispatchQueue.global():
.continueWith(executor: AWSExecutor(dispatchQueue: DispatchQueue.global())) {
Sample code diff: https://github.com/joaomarceloods/AWSTranscribeBug/commit/98e43af553413ed2bbd0c3f96e259139a991303e
Reference: https://aws-amplify.github.io/docs/ios/how-to-ios-asynchrounous-tasks#executing-a-block-on-the-main-thread-with-awstask

How to test if Xamarin Android app closes

I am writing my first Android app, using Xamarin. I have an Exit button that, when clicked, closes the app. I want a test in Xamarin UITest that verifies clicking the button closes the app. I messed around with it for a while and finally found something that allows the test to pass.
In the app:
exitButton.Click += (o, e) =>
{
int pid = Android.OS.Process.MyPid();
Android.OS.Process.KillProcess(pid);
};
In UITest:
[Test]
public void ExitButtonClosesTheScreen()
{
try
{
app.Tap(c => c.Button("exitButton"));
Assert.Fail("App remains open.");
}
catch (System.Exception e)
{
Assert.AreEqual("The underlying connection was closed: The connection was closed unexpectedly.", e.InnerException.InnerException.InnerException.Message);
}
}
The test now passes so I guess I'm happy. My question is, is this really the best way to do this? Or is there a better way that I wasn't able to find?
Edit: Unfortunately, this is not the answer. This method allows the test to pass in VS but fails when I run it in App Center. Is there another way to run this test? Or is this something that is simply not testable with UITest? Thank you.
First of all the right code for closing the Application as per me is using finish affinity
In an Activity:
this.FinishAffinity();
In a Fragment:
this.Activity.FinishAffinity();
After doing this AppCenter should be able to figure that your app is closed.
I did a brief read up on this the other day for something similar and I am certain that the ActivityManager class would be the best way to go about this.
https://developer.xamarin.com/api/type/Android.App.ActivityManager/
There is a method within this class called RunningAppProcesses which returns a list of application processes that are running on the device - and from there I guess you can assert if your app process is on the list or not.
Hope this helps
After almost 4 years, i've encountered with the same issue.
I will do it this way in your case:
[Test]
public void ExitButtonClosesTheScreen()
{
app.Tap(c => c.Marked("exitButton"));
/** I asume exitButton click action will just exit,
no popups or alerts appear before exiting. **/
app.WaitForNoElement(q => q.Marked("exitButton"),
"Timeout waiting for element exitButton",
new TimeSpan(0, 0, 30));
AppResult[] result = app.Query();
Assert.IsTrue(result.Length == 0);
}
app.Query() returns all views visible by default, unless a query is especified by a lambda expression, as you should alredy know.
If the Application is gone, the Views visible will be 0, and as such, app.query() will return and array lenght of 0.
For WaitForNoElement's timeout I use a TimeSpan of 30 seconds, but you can use whatever timeout you prefer for this operation, i just considered 30 seconds will be ok.

CloudKit method call hung up

When app starts some preliminary process take place. Sometimes it is done quickly in some second, and sometimes It does not end, but without any error it hung up.
I.e. at launch client always fetch the last serverChangedToken. and sometimes it just hung up it does not complete. I am talking about production environment, developer works well. So this route get called, but some times it does not finishes. Any idea why? I do not get any error, timeout.
let fnco = CKFetchNotificationChangesOperation(previousServerChangeToken: nil)
fnco.fetchNotificationChangesCompletionBlock = {newServerChangeToken, error in
if error == nil {
serverChangeToken = newServerChangeToken
dispatch_sync(dispatch_get_main_queue(), {
(colorCodesInUtility.subviews[10] ).hidden = false
})
} else {
Utility.writeMessageToLog("error 4559: \(error!.localizedDescription)")
}
dispatch_semaphore_signal(sema)
}
defaultContainer.addOperation(fnco)
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER)
I know it is not recommended to use semaphores to control flow of the CloudKit method calls.
Do you think the last two line can be swapped? First dispatch_semaphore_wait and then addOperation be called?
Strange that app worked for iOS 8, this bug arise only in iOS 9.
Adding the following line of code will probably solve your problem:
queryOperation.qualityOfService = .UserInteractive
In iOS 9 Apple changed the behavior of that setting. Especially when using cellular data you could get the behaviour you described.
The documentation states for the .qualityOfService this:
The default value of this property is NSOperationQualityOfServiceBackground and you should leave that value in place whenever possible.
In my opinion this is more a CloudKit bug than a changed feature. More people have the same issue. I did already report it at https://bugreport.apple.com

Dart Web Server: prevent crash

Id'like to develop a web services + web sockets server using dart but the problem is I can't ensure the server's high availability because of uncatched exceptions in isolates.
Of course, I have try-catched my main function, but this is not enough.
If an exception occurs in the then() part of a future, the server will crash.
Which means that ONE flawd request can put the server down.
I realize that this is an open issue but is there any way to acknoledge any crash WITHOUT crashing the VM so that the server can continue serving other requests ?
Thank you.
What I've done in the past is use the main isolate to launch a child isolate which hosts the actual web server. When you launch an isolate, you can pass in an "uncaught exception" handler to the child isolate (I also think you should be able to register one at the top-level as well, to prevent this particular issue, as referenced by the issue in the original question).
Example:
import 'dart:isolate';
void main() {
// Spawn a child isolate
spawnFunction(isolateMain, uncaughtExceptionHandler);
}
void isolateMain() {
// this is the "real" entry point of your app
// setup http servers and listen etc...
}
bool uncaughtExceptionHandler(ex) {
// TODO: add logging!
// respawn a new child isolate.
spawnFunction(isolateMain, uncaughtException);
return true; // we've handled the uncaught exception
}
Chris Buckett gave you a good way to restart your server when it fails. However, you still don't want your server to go down.
The try-catch only works for synchronous code.
doSomething() {
try {
someSynchronousFunc();
someAsyncFunc().then(() => print('foo'));
} catch (e) {
// ...
}
}
When your async method completes or fails, it happens "long" after the program is done with the doSomething method.
When you write asynchronous code, it's generally a good idea to start a method by returning a future:
Future doSomething() {
return new Future(() {
// your code here.
var a = b + 5; // throws and is caught.
return someAsyncCall(); // Errors are forwarded if you return the Future directly.
});
}
This ensures that if you have code that throws, it catches them and the caller can then catchError() them.
If you write this way, you have much less crashes, assuming that you have some error handling at the top level at least.
Whenever you are calling a method that returns a Future, either return it directly (like shown above) or catchError() for it so that you are handling the possible errors locally.
There's a great lengthy article on the homepage that you should read.

Resources