Please point me in the correct direction to understand how to run in the background in iOS - ios

I'm working on a fitness app (using Xamarin) and would like it to continue running in the background if the user switches to another app or pushes the side button to blank out the screen. I have looked at multiple tutorials, include this one https://robgibbens.com/backgrounding-with-xamarin-forms/, which seemed very promising. However, when running my app and switching to another application or blanking the screen, my app simply suspends. Hers is the iOS specific code that starts the background task:
public class IOSPlayWorkoutTask
{
nint _taskID;
CancellationTokenSource _cts;
Workout _workout;
public async Task Start()
{
_cts = new CancellationTokenSource();
_taskID = UIApplication.SharedApplication.BeginBackgroundTask("IOSPlayWorkoutTask", OnExpiration);
try
{
WorkoutPlayer.Shared.Workout = _workout;
await WorkoutPlayer.Shared.PlayWorkout(_cts.Token);
}
catch (OperationCanceledException)
{
}
finally
{
if (_cts.IsCancellationRequested)
{
//Device.BeginInvokeOnMainThread(
// () => MessagingCenter.Send(new CancelPlayingWorkoutMessage(), CancelPlayingWorkoutMessage.MessageText));
}
}
UIApplication.SharedApplication.EndBackgroundTask(_taskID);
}
public void Pause()
{
WorkoutPlayer.Shared.RequestPause();
_cts.Cancel();
Device.BeginInvokeOnMainThread(
() => MessagingCenter.Send(new PausedWorkoutMessage(), PausedWorkoutMessage.MessageText));
}
public void Stop()
{
_cts.Cancel();
Device.BeginInvokeOnMainThread(
() => MessagingCenter.Send(new FinishedWorkoutMessage(), FinishedWorkoutMessage.MessageText));
}
void OnExpiration()
{
_cts.Cancel();
}
public IOSPlayWorkoutTask(Workout w)
{
_workout = w;
}
}
The iOS app delegate registers to receive messages, one of which starts the task above. I know I must be missing something very basic. Any help would be greatly appreciated. I know this has to be possible based on my experience using other fitness apps like Couch to 5k.

Thanks #sushihangover and #Paulw11 for your input. Looks like the secret is adding a handler to BeginBackgroundTask that calls EndBackgroundTask. Not sure I understand fully at this point, but apparently this give iOS the impression that you are a "good citizen", but continues processing your task. Here is my code that works:
public async Task Start()
{
_cts = new CancellationTokenSource();
//_taskID = UIApplication.SharedApplication.BeginBackgroundTask("IOSPlayWorkoutTask", OnExpiration);
_taskID = UIApplication.SharedApplication.BeginBackgroundTask(() =>
{
Console.WriteLine("Guess my time is up");
UIApplication.SharedApplication.EndBackgroundTask(_taskID);
});
try
{
WorkoutPlayer.Shared.Workout = _workout;
await WorkoutPlayer.Shared.PlayWorkout(_cts.Token);
//finished = true;
}
catch (OperationCanceledException)
{
Console.WriteLine("OperationCanceledException");
//finished = true;
}
finally
{
if (_cts.IsCancellationRequested)
{
//Device.BeginInvokeOnMainThread(
// () => MessagingCenter.Send(new CancelPlayingWorkoutMessage(), CancelPlayingWorkoutMessage.MessageText));
}
}
UIApplication.SharedApplication.EndBackgroundTask(_taskID);
}

Related

can I know when a StreamSubscription is cancelled?

is there anything like onCancel for a StreamSubscription?
example:
var subscription = someStream.listen((item) => null);
subscription.cancel(); // does this trigger any event?
I ended up creating a _StreamSubscriptionDelegate that delegates all methods and so I can put some logic when the subscription is cancelled, however, maybe there is an easier solution to it.
If the stream comes from a StreamController, then the controller is notified of the cancel. The listener is expected to keep track of their own subscription, so if one part of the client code needs to know that another part has cancelled the stream, then wrapping the subscription in something which records that you cancelled it, is a perfectly good approach.
Another approach could be to wrap the stream before listening to it:
Stream<T> onCancel<T>(Stream<T> source, void onCancel()) async* {
bool isCancelled = true;
try {
await for (var event in source) {
yield event; // exits if cancelled.
}
isCancelled = false;
} finally {
if (isCancelled) onCancel();
}
}
or
Stream<T> onCancel<T>(Stream<T> source, void onCancel()) {
var sink = StreamController<T>();
sink.onListen = () {
var subscription = source.listen(sink.add, onError: sink.onError, onDone: sink.close);
sink
..onPause = subscription.pause
..onResume = subscription.resume
..onCancel = () {
subscription.cancel();
onCancel();
};
};
return sink.stream;
}

When do Stream start publishing values to listeners?

After reading a bunch of documentation about Streams and StreamControllers in dart I tried to build a little example and was surprised of the results. All documentation I have read states that a stream starts emiting data as soon as a listener is registered. But this doesn't show any printed data:
class Order
{
String type;
Order(this.type);
}
class Pizza
{
}
void main()
{
Order order = Order("pzza");
final StreamController sc = StreamController();
sc.sink.add(order);
sc.sink.add(order);
sc.sink.add(new Order("pizza"));
Stream st = sc.stream.map((order) {
return order.type;
})
.map((orderType) {
if(orderType == "pizza")
return Pizza();
else
throw ("dude!, I don't know how to do that");
});
var sus = st.listen((pizza)
{
print("We did a pizza");
},
onError: (error)
{
print(error);
});
sus.cancel();
sc.sink.add(new Order("pizza2"));
}
I was expecting this output:
dude!, I don't know how to do that
dude!, I don't know how to do that
We did a pizza
When creating streams and adding data is all "sinked" data scheduled to be emited on the next application step?
Cheers.
You are right in that the documentation states that you listen on a stream to make it start generating events. However, streams are asynchronous so when you call the listen() method you are registering to receive events from the stream at some point in the future. Dart will then continue to run the remainder of your main function. Immediately after calling listen() you call cancel() to cancel the subscription which is why nothing is being printed.
If you remove or comment out the cancel and run it again you will see the expected output.
A slightly modified version of your code will hopefully highlight the run of events:
class Order {
String type;
Order(this.type);
}
class Pizza {}
void main() {
print("Main starts");
Order order = Order("pzza");
final StreamController sc = StreamController();
sc.sink.add(order);
sc.sink.add(order);
sc.sink.add(new Order("pizza"));
Stream st = sc.stream.map((order) {
return order.type;
}).map((orderType) {
if (orderType == "pizza")
return Pizza();
else
throw ("dude!, I don't know how to do that");
});
var sus = st.listen((pizza) {
print("We did a pizza");
}, onError: (error) {
print(error);
});
// sus.cancel();
sc.sink.add(new Order("pizza2"));
print("Main ends");
}
Running this produces the output:
Main starts
Main ends
dude!, I don't know how to do that
dude!, I don't know how to do that
We did a pizza
dude!, I don't know how to do that

Xamarin-IOS BTLE WroteCharacteristicValue not fired

I have the following code for my IOS implementation, the problem is that the WroteCharacteristicValue event is never fired. Is is being fired on the android side when I connect to the same module. Any ideas what to do?
public void StartUpdates ()
{
// TODO: should be bool RequestValue? compare iOS API for commonality
bool successful = false;
if(CanRead) {
Console.WriteLine ("** Characteristic.RequestValue, PropertyType = Read, requesting read");
_parentDevice.UpdatedCharacterteristicValue += UpdatedRead;
_parentDevice.ReadValue (_nativeCharacteristic);
successful = true;
}
if (CanUpdate) {
Console.WriteLine ("** Characteristic.RequestValue, PropertyType = Notify, requesting updates");
_parentDevice.UpdatedCharacterteristicValue += UpdatedNotify;
_parentDevice.WroteCharacteristicValue += Wrote; // -DP here??
_parentDevice.SetNotifyValue (true, _nativeCharacteristic);
successful = true;
}
Console.WriteLine ("** RequestValue, Succesful: " + successful.ToString());
}
void Wrote(object sender, CBCharacteristicEventArgs e) {
System.Diagnostics.Debug.WriteLine("Characteristic Write Complete!");
this.WriteComplete (this, new CharacteristicReadEventArgs () {
Characteristic = new Characteristic(e.Characteristic, _parentDevice)
});
}
The WroteCharacteristic will only fire if the characteristic writes with response.
You can check it with:
var prop = _nativeCharacteristic.Properties;
if(prop.HasFlag(CBCharacteristicProperties.Write))
{
// Event can be used
}
else if(prop.HasFlag(CBCharacteristicProperties.WriteWithoutResponse))
{
// Event will not fire if WriteWithoutResponse
}
Btw: we provide a plugin for BLE, so you don't have to care about platform sepcific stuff ;) http://smstuebe.de/2016/05/13/blev1.0/

Why Facebook profile picture request return null on iOS?

Everything works perfect on android but when I try to get the profile picture on iOS devices. The image returns null. I checked the Facebook documentation for iOS 9 I have exactly the same plist as shown in documentation. When I run the app in console I see "FB is log in" message but the profile pic has not shown. Can anyone help?
void Awake()
{
instance = this;
FB.Init(SetInıt, OnHideUnity);
}
public void FbLogin()
{
// This is an event trigger when the button pressed.
List<string> permissions = new List<string>();
permissions.Add("public_profile");
FB.LogInWithReadPermissions(permissions, AuthcallBack);
}
void DealWithFbMenus(bool isLoggedIn)
{
// This function is called in SetInit func in Awake.
if(isLoggedIn)
{
fbButton.SetActive(false);
profilePicture.gameObject.SetActive(true);
loggedInPlayer = true;
//FB.API("/me?fields=first_name", HttpMethod.GET, DisplayUserName);
FB.API("/me/picture?type=square&height=128&width=128", HttpMethod.GET, DisplayProfilePic);
}
else
fbButton.SetActive(true);
}
void DisplayProfilePic(IGraphResult result)
{
if(result.Texture != null)
{
profilePicture.sprite = Sprite.Create(result.Texture, new Rect(0,0, 128, 128), new Vector2());
}
}
It is a bug on Unity 5.2. It fixed on new version of Unity 5.3

calling a webservice from scheduled task agent class in windows phone 7.1

Can we call a webservice from the scheduled periodic task class firstly, if yes,
Am trying to call a webservice method with parameters in scheduled periodic task agent class in windows phone 7.1. am getting a null reference exception while calling the method though am passing the expected values to the parameters for the webmethod.
am retrieving the id from the isolated storage.
the following is my code.
protected override void OnInvoke(ScheduledTask task)
{
if (task is PeriodicTask)
{
string Name = IName;
string Desc = IDesc;
updateinfo(Name, Desc);
}
}
public void updateinfo(string name, string desc)
{
AppSettings tmpSettings = Tr.AppSettings.Load();
id = tmpSettings.myString;
if (name == "" && desc == "")
{
name = "No Data";
desc = "No Data";
}
tservice.UpdateLogAsync(id, name,desc);
tservice.UpdateLogCompleted += new EventHandler<STservice.UpdateLogCompletedEventArgs>(t_UpdateLogCompleted);
}
Someone please help me resolve the above issue.
I've done this before without a problem. The one thing you need to make sure of is that you wait until your async read processes have completed before you call NotifyComplete();.
Here's an example from one of my apps. I had to remove much of the logic, but it should show you how the flow goes. This uses a slightly modified version of WebClient where I added a Timeout, but the principles are the same with the service that you're calling... Don't call NotifyComplete() until the end of t_UpdateLogCompleted
Here's the example code:
private void UpdateTiles(ShellTile appTile)
{
try
{
var wc = new WebClientWithTimeout(new Uri("URI Removed")) { Timeout = TimeSpan.FromSeconds(30) };
wc.DownloadAsyncCompleted += (src, e) =>
{
try
{
//process response
}
catch (Exception ex)
{
// Handle exception
}
finally
{
FinishUp();
}
};
wc.StartReadRequestAsync();
}
private void FinishUp()
{
#if DEBUG
try
{
ScheduledActionService.LaunchForTest(_taskName, TimeSpan.FromSeconds(30));
System.Diagnostics.Debug.WriteLine("relaunching in 30 seconds");
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
#endif
NotifyComplete();
}

Resources