Display map after page navigation completes - flutter

In my app I navigation to a page with a fullscreen map. Currently I've implemented a FutureBuilder to render the map or else a progressindicator is displayed. From initState I request UserLocation (lastknown or current) and the maps navigates to the location. The issue is that the page navigation is not smooth after I added the map. I'm looking for a way to make the page complete its navigation before the map is rendered. I've searched but haven't found a working solution yet. Any good ideas on how to make this?
Happy Flutter day :)

Use Isolates to do any intensive work. An Isolate is essentially a new thread so that the main thread is freed up to continue doing UI work.
from https://flutter.io/docs/resources/faq#how-do-i-write-parallel-andor-concurrent-apps-for-flutter
How do I write parallel and/or concurrent apps for Flutter? Flutter
supports isolates. Isolates are separate heaps in Flutter’s VM, and
they are able to run in parallel (usually implemented as separate
threads). Isolates communicate by sending and receiving asynchronous
messages. Flutter does not currently have a shared-memory parallelism
solution, although we are evaluating solutions for this.
Check out an example of using isolates with Flutter.

Related

Future builder with page transition cause lag

I have some troubles in my applications since I'm using different animated widget in combination of Future Builder (I think this is the problem).
For example when I perform a navigation to another page and in this page I retrieve data with future builder, the page transitions lag. This is an example:
As you can see the animation is lagging at the end. But I'm not sure that is a problem with Future Builder and the transitions.
This example was made on a release application on iOS, so not in debug mode.

Flutter detect if screen off

I am currently writing an app, and records some info (eg GPS), even if screen is off (ie its screen has timed out in sleep mode).
It performs a setState() every so often to update the Widgets.
However, if the screen is asleep/inactive, I'm not sure there is a need to call setState(), or certain other cpu intensive actions, and may be better to preserve battery by not calling it ?
So, I am wondering if there is any way to detect if the screen is off, and hence not call setState..
eg in pseudocode
if( checkScreenIsOn() ) { setState((){...})}
I'm unsure if some event is triggered when the screen goes off that flutter sees. Also if things like GPS, I'd like to record normally when the screen is off (I'm currently using a listener), but not update the display. Does GPS go into a different mode, and any way to stop that if so ?
StatefullWidget have a mounted flag mounted property
Instead of if (checkScreenIsOn()) just do if (mounted) setState((){...})
If you want to know screen state you must use a MethodChannel to communicate with native api, eg. https://pub.dartlang.org/packages/screen_state

Adding a lot of data to core data while keeping the UI responsive

I'm looking for a way to add a whole load of data to core data while keeping the little activity indicator spinning on the UI. I tried adding the data on another thread but since learned that core data is not thread safe, and I get all kinds of errors. Can anyone suggest another approach to this?
Thanks in advance.
Core Data is not thread-safe, but that just means you have to code appropriately, rather avoiding them entirely. To keep your UI responsive/up-to-date, you'll need to use threads.
Apple's documentation on the subject is here, and this blog post is an excellent walk-through of using multiple threads with Core Data, and some of the pitfalls involved.
You need to use a separate managed object context for each thread. There's some additional work you need to do to make changes from other contexts available to your main thread's context: see Concurrency with Core Data for a full discussion.
Most useful example is a core data XML downloading from iOS sample code. If u don't have access, please let me know and I will write here is a main structure of it.
There is a complex custom delegate techniques from u UIviewController to nsoperation delegate, this is important for u to start showing content immediately to user and show a progress of sync.
In AppStore u can see my "snow IXC" app, where u can see this techniques implemented for indicate user in their UIviewController about progress. It's free for downloading.
This is how I avoid background threads with a loading indicator (I use DSActivityView but this should work with other implementations):
In your code when you are going to be displaying the indicator run all the code to show the indicator first. After that code have a separate method call to do all the loading work. Call it by using the method:
[self performSelector:#selector(loadMethodName) withObject:nil afterDelay:0.0];
Normally the app will go straight into the loading code without waiting to show the indicator view. By calling it this way it will first finish displaying the indicator before it moves into the loading code.

How to avoid UI freezes?

Im trying to find the best way to avoid having tiny UI lockups after certain events in my app. For example here are two cases where i have come across mini-lockups:
I have a button that when pressed loads a local mp3 file (around 20-30mb) using AVAudioPlayer, calls the prepareToPlay method and then plays the audio file. The issue is that the button has the showsTouchWhenHighlighted flag set to yes and after pressing it it stays highlighted until the audio file begins playing which could take 1-3 secs.
Another similar example is using a navbar button to sort and reload the table data. There is a very short but noticeable lockup where the button remains highlighted until the table has reloaded.
Can anyone suggest a good approach to minimizing/eliminating these mini lockups? I didnt really come across any code to achieve this in any of the books/tutorials i read that dealt with tableviews and avaudioplayer. Should i be launching these actions on different threads?
thx
For number 1, your best bet is to load the item on a background thread and inform the user that you're doing so (either via a loading HUD or some other indicator). The simplest way of doing this will be to use - (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg;. If you're running iOS 4.0 + you may want to look into executing block callbacks and see if they will work for you.
For number 2, perform the sorting on the background thread or change your sorting method (consider using a sorted data structure rather than resorting after inserts?). reloadData should occur only on the main thread.
I'm an iPhone noob myself, but it sounds like your code is stuck doing synchronous actions, which will guarantee that your UI gets locked up until the action is done executing.
Although I don't have an explicit answer, look for asynchronous options to perform these actions as they will not lockup your UI. These are usually achieved through using threads or deferred objects. From my experience so far w/ Objective-C, most actions your program neds to take can be achieved through async actions.

Using the webView method, "loadRequest", do I need to worry about threading? (iphone sdk question)

Lets suppose I am creating an application for the iphone with a webView down at the bottom of the window (the other part of the screen has a button and the user can interact with it).
I don't want the webView to stop the user from interacting with the other part of the UI when the webView loads a new url. From my limited testing through the iphone simulator, I haven't been able to determine IF it already behaves this way. Most of my web sites load pretty fast.
I seem to be able to load new requests and click the ui button while that happens.
So, again, do I need to worry about threading in this case?
No, you do not. The iPhone threads a great deal of the UI components behavior, or schedules them for you in the main run loop in such a way that you rarely need to be concerned, the UI elements will be available for user interaction.