I have an app which build on Xcode objective-c, I have a code and I need that code to run even if the user press the home button.
Is it possible to do it?
Refer to the Background Execution chapter of the App Programming Guide for iOS.
There are three difference scenarios for background network requests:
The user initiates a simple request and expect the server to respond reasonably quickly, but want to make sure that if the user leaves the app before the request complete, that it really has a chance to finish gracefully in the background.
See the Executing Finite-Length Tasks section of the aforementioned guide for a discussion on how to request a little extra minutes after the user leaves the app, and that may be sufficient to finish the network request.
You are requesting large volumes of data (or uploading a lot of data), where it is anticipated to possibly require more than a few minutes to finish, especially on slow connection.
In this case, as Phillip Mills pointed out, you can use a background NSURLSession (as discussed in the Background Transfer Considerations section of the URL Loading System Programming Guide: Using NSURLSession guide.
You want to periodically make very quick calls to your web service to check to see if there is any new data, even if the user isn't using your app at the time.
In this case, you should look into "Background Fetch". See the Fetching Small Amounts of Content Opportunistically section of the App Programming Guide for iOS. You can't control precisely when it checks, but it is a way to initiate short network requests even when the app isn't currently running.
Note, if this opportunistic background fetch determines that there is a large volume of data to be downloaded, you can combine this pattern with the previous pattern (the background NSURLSession I discussed in point #2).
For more information on this, see the WWDC 2013 video, What's New with Multitasking.
Related
Across all browsers/devices, I find random different pages, at random times, are very slow to load/don't load. The browser is stuck on 'Waiting for website.com'. I will wait 20 seconds and nothing will happen until I manually refresh the page. As I realise this is very vague, can you suggest a) most likely issues to look for first or b) some diagnostic tools that I could use to try and de-bug the issue as a starting point, so that my hosts/developers can solve the issue. Here are some results of recent speed tests.
One thing to also add is that, it seems it more often gets stuck on particular pages. Namely the pages where users take practice tests. After each time the user clicks 'Next', their selected answer is inserted into the database. My speculation is that potentially it's an issue with the DB itself, or the process which inserts into the database. It's when clicking 'Next', that the whole website sometimes just dies as described above.
Results from Google Speed Test
Waterfall image
A wait time of 20secs at random times and random pages could possibly be due to stop-the-world garbage collection. So GC logs are probably a good starting point.
A thread sampler such as Djigger a colleague of mine wrote might probably also help you figuring out what the machine is doing during the 20 seconds.
If that doesn't help I suggest to use a Profiler or better an APM tool to monitor whats going on on your system. Those tools give a you a broader insight of the internals.
You need to run a few page speed tests and look at the waterfall images.
It is very common on shared servers for the server to be too busy to get to your request. 20 seconds would indicate a serious issue with the server.
Another common reason is the page has a link to a third party resource and that resource is too often unavailable.
In your case the culprit is website.com and I assume that is your site.
Use something like webpagetest.org to run the tests.
In the waterfall image below
Dark Green is DNS lookup time.
Orange is the time for Browser to connect to server.
Green is the wait time for server to put image in output buffer.
Blue is the time for the server to transmit to the Browser.
The problem with the sample waterfall page is the index page took 4 seconds to be generated or retrieved. Most likely this is a Word Press site with plugins.
I suspect yours may be 20 seconds. But due to the randomness, is is also a good possibility it is a page resource that is stalled.
If it is the index page, then you likely have a poor ISP and or one of the other users of the server is hogging the CPU.
Keep running the tests until you see the problem occur.
It will be very obvious where the problem is located.
You can post the waterfall image and send me a message if you have any questions.
Waterfall from webpagetest.org
Use a StackOverflow Q&A thread as an example - when you vote up, vote down, or favorite a question, you can see the UI quickly respond to that action with changes in the # of up-votes on the side.
How can we achieve that effect? If send every of such action to back-end for processing and use the returned response to update UI, you will see a slow update and feel the glitches. But if put some of the logic on the front-end, you will also need to take care of the fraud/abuse etc before reflecting the action on UI, i.e - before changing the # of up-votes, don't you need to make sure that's a valid click by an valid user first?
You make sure that a valid user is using the app before a user clicks on anything. This is done through authentication, and it must include various protection mechanisms against malicious users.
When a user clicks, a call is made to a server. In a properly architected app this call is lightweight, and the server responds very quickly. I don't know why you believe that "you will see a slow update and feel the glitches". Adding an upvote to the database should take a few hundred milliseconds at most (including the roundtrip from the client), especially if the commit is asynchronous or a memcache is used.
If a database update results in a need to do some complex operations, typically these operations are not done right away. For example, a cron job may run periodically to compute new rankings, etc., precisely because you do not want every user to wait. Alternatively, a task is created and put in a task queue to be executed when resources are available - again to make sure that a user does not wait.
In some apps a UI is updated immediately after the call to the server is made, before any response from a server arrives. You can do it when the consequences of a failed call are negligible. For example, if an upvote fails to be saved in the database, it's not a disaster, especially if it happens once in a million tries. Again, in a properly architected app calls fail extremely rarely.
This is a decision that an app developer needs to make. I would not update a UI before a server response if such an update may lead a user to believe that some other action is now possible. For example, if a user uploads a new photo, I would not show icons to edit or share this photo until I know that the photo is safely saved.
I have an app which needs almost no user interaction, but requires Geofences. Can I run this entirely within a background service?
There will be an Activity when the service is first run. This Activity will start a service and register a BroadcastReceiver for BOOT_COMPLETED, so the service will start at boot. It's unlikely that this Activity will ever be run again.
The service will set an Alarm to go off periodically, which will cause an IntentService to download a list of locations from the network. This IntentService will then set up Geofences around those locations, and create PendingIntents which will fire when the locations are approached. In turn, those PendingIntents will cause another IntentService to take some action.
All this needs to happen in the background, with no user interaction apart from starting the Activity for the first time after installation. Hence, the Activity will not interact with LocationClient or any location services.
I've actually got this set up with proximityAlerts, but wish to move to the new Geofencing API for battery life reasons. However, I have heard that there can be a few problems with using LocationClient from within a service. Specifically, what I've heard (sorry, no references, just hearsay claims):
location client relies on ui availability for error handling
when called from background thread, LocationClient.connect() assumes that it is called from main ui thread (or other thread with event looper), so connection callback is never called, if we call this method from service running in background thread
When I've investigated, I can't see any reason why this would be the case, or why it would stop my doing what I want. I was hoping it would be almost a drop-in replacement for proximityAlerts...
Can anyone shed some light on things here?
The best thing would be to just try it out, right? Your strategy seems sound.
when called from background thread, LocationClient.connect() assumes that it is called from main ui thread (or other thread with event looper), so connection callback is never called, if we call this method from service running in background thread.
I know this to be not true. I have a Service that is started from an Activity, and the connection callback is called.
I dont know about proximity alerts; but I cant seem to find an API to list my GeoFences. I am worried that my database (sqlite) and the actual fences might get out of sync. That is a design flaw in my opinion.
The reason LocationClient needs UI, is that the device may not have Google Play Services installed. Google has deviced a cunning and complex mechanism that allows your app to prompt the user to download it. The whole thing is horrible and awful in my opinion. Its all "what-if what-if" programming.
(They rushed a lot of stuff out the door for google IO 2013. Not all of it are well documented, and some of it seems a bit "rough around the edges").
I am trying to find out if any http requests are made during installation of an msi package. It appears to me that the process under which the http request is made shares no lineage with the process under which the installer executes.
For example, I install an app that makes http calls during installation. Using SysInternals process monitor, I see the process created when the install kicks off. Using MS NetworkMonitor I can see the process used to generate the http request. Filtering in Process Monitor after the fact shows that there is no relationship between the http process, and the install process.
I am thinking that somehow the OS says to use a new process whenever an http request is made. My most important requirement is that I be able to relate one to the other, in order to definitively say "This app installation called these http resources during install". So I don't have to have a perfect understanding of how it all works under the covers, but, I am at a standstill right now. I've concluded that there is no way to relate the two. Am I wrong?
Okay, let's assume that msiexec.exe invokes a helper and that helper invokes whatever is causing the HTTP traffic. Right after the first helper spawns the child it kills itself. This process is too short-lived to normally see the relationships here.
Enter the "Process Tree" feature of Process Monitor. Keep Process Monitor running without any filters on process events. After you are done you can then press Ctrl+T to see the Process Tree (see below).
The grayed icons tell you the process is not active anymore. Furthermore the last column is the end time of the process. But best of all you can see which process created which other process from this, even for very very short-lived processes.
Mark Russinovich, author of Process Monitor and its predecessors, demonstrated this at TechEd about a month ago.
Although this may not answer the question entirely, it should get you going in the right direction. After all Process Monitor also includes network activity filtering (albeit crude, compared to Network Monitor and Wireshark :)).
btw: the green bar in the above screenshot is the "timeline" where you can see the runtime of the process in relation to other processes. Very nifty.
Are you using a custom action for making web requests? Windows Installer Service Process runs custom actions in a separate instance of MSIEXEC than your main set-up instance.
More information here:
http://blogs.msdn.com/b/astebner/archive/2005/03/02/384088.aspx
I have a site that has to crawl different sites to aggregate information. When the crawling scripts are running, the site's speed slows down. I have done as much as possible to optimize the crawling, but it's really CPU- and RAM- intensive. These crawls have to occur based on some user action (e.g. search). It is not an option to "pre-crawl" the information as the information is time-sensitive.
What are the general strategies I can use to solve this? Here are 2 of my ideas:
Get more CPU and RAM on current server
Offload these processing intensive scripts on a separate physical server
I'm wondering about cloud computing, but don't have any experience in it. Suggestions?
You've already identified the options. "Cloud computing" doesn't mean anything but being able to quickly allocate a VPS with hourly pricing. It's the same as buying another physical server, except without waiting for the host to put it online and e-mail you access info, and without a monthly commitment. You still have to write your application to make use of multiple servers, you have to write code to "scale up" or "scale down" as needed (purchase or terminate virtual servers, and write code to automatically start whatever programs you need on them), you still have to properly manage the servers (install and maintain an OS, keep packages updated with security fixes) etc.
You could try to make the action to be asynchronous:-
User submits a search.
System displays "The system is currently searching the information based on your criteria and you will be notified shortly". System handles the user request at the mean time.
Since the user isn't waiting for the result page, the user is free to browse around or do other thing in your website instead of locking up their screens.
When the result is generated, system notifies the user that the search is done and provides the link for the user to view the result. This can be done by either sending an email notification to the user, or merely popping a dialog box or sliding down a notification message on the menu bar (basically something to catch the user attention).
It is wise to have a separate machine to run these processing intensive scripts so that it will not slow down the entire application server especially when you have tons of users submitting the search.