I have a datafield. I am trying to add a background process to download some json file from the web. (I know new devices can do it in the foreground, but I also want it to work for older devices).
So I added (:background) annotation to the class of MyApp:
import Toybox.Application; import Toybox.Lang; import Toybox.WatchUi; (:background) class MyApp extends Application.AppBase { hidden var view as MyView?; function initialize() { AppBase.initialize(); } (:typecheck(false)) function getInitialView() as Array<Views or InputDelegates>? { self.view = new MyView(); return [ self.view ] as Array<Views or InputDelegates>; } (:foreground) function onSettingsChanged() as Void { if (view != null && !isRunningInBackground()) { view.onSettingsChanged(SETTINGS_CHANGED_FROM_CONNECT_IQ); } } } (:no_ciq_2_4_0, :background) function isRunningInBackground() as Boolean { try { var app = getApp(); app.setProperty(PROPERTY_NON_EXSISTANT, 1); app.deleteProperty(PROPERTY_NON_EXSISTANT); return false; } catch (ex) { return true; } } (:ciq_2_4_0, :background) function isRunningInBackground() as Boolean { try { Properties.setValue(PROPERTY_EXSISTANT, false); return false; } catch (ex) { return true; } }
Until now in onSettingsChanged it called view.onSettingsChanged which was always set (IMHO), but now when the background app will run there won't be a view obviously.
The (:foreground) annotation doesn't help, I guess the whole class is in the background code, so if the onSettingsChanged method is overridden then it calls it. I get the following compilation errors. How can I make this compile?
ERROR: fr955: MyApp.mc:68,12: Value 'onSettingsChanged' not available in all function scopes.
ERROR: fr955: MyApp.mc:68,12: Cannot find symbol ':onSettingsChanged' on type 'Null'.
ERROR: fr955: MyApp.mc:68,12: Value 'SETTINGS_CHANGED_FROM_CONNECT_IQ' not available in all function scopes.
The 2nd error is just a bug in the compiler, as it should know that view isn't null, but what can I do if I don't want this code to run at all if the background?