What is NSRunLoop? [duplicate] - ios

This question already has answers here:
Understanding NSRunLoop
(6 answers)
Closed 9 years ago.
I have been reading some documents about run loop, but still can not understand it exactly.
As iOS is not open source, while NSRunLoop is special for iOS/Mac OS X platform, what is its real implementation inside?

If you have some kind of user interface, or other code that needs to listen to events (like network ports), you need a run loop. Every NSThread automatically gets its own run loop, and you very rarely have to concern yourself with them directly. The run loop is also in charge of creating and releasing autorelease pools.
For more information :
What is the basic difference between NSTimer, NSTask, NSThread and NSRunloop?

Related

iOS: Execute code in regular intervals (or irregular intervals) [duplicate]

This question already has answers here:
iOS do scheduled operation in background or when app active
(3 answers)
Run a task at fixed time everyday
(1 answer)
Closed 2 years ago.
I am creating an app that should check something in regular intervals. (Not a service call just harmless check which won't take much of CPU usage)
To run a code in background for a fixed interval of time, I tried using BackgroundTasks framework. My idea was to schedule one background AppRefresh task and schedule another when the first one gets called. Also in the whole day I tried with this framework, there were no updates. Though I always scheduled to run my job after 5 minutes, I sometimes waited for an hour for updates.
The code is working, I used the console command to simulate the background task and it worked.
I am not sure what framework I should use to achieve this?

Synchronous HTTP Requests in Swift [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Is it ok to wait for an asynchronous request (say an NSURLSessionDataTask,) using a semaphore for example.
There is a lot of advice out there claiming that synchronous networking is bad, however I don't see that as grounds for refusing synchronous requests, especially when they're done in a background thread.
Synchronous requests (in a background thread/queue) have the benefit of not needing to be callback-nested.
The answer in https://stackoverflow.com/a/31563134/466604 shows only a method to achieve this, but it comes with implied discouragement. (Along with other discouragement from https://devforums.apple.com/thread/9606?tstart=0)
My question is whether discouraging this discouragement is warranted, given those synchronous requests happen in a background thread, primarily as a way to avoid nesting callbacks.
No, it's not ok. Don't do this.
iOS might even kill your app in the process. Besides, look at callbacks as they were the next step in your serial execution.
The processor needs those cycles to do other things, don't be a bully!
INSIGHT:
As #EricD said, if your execution fails for some reason, that thread is lost until you exit your app and the O.S. claims it back.
That thread is a valuable resource that must not be wasted and much
more valuable in Mobile Operating Systems as iOS.
iPhone's processors are not as powerful as nowadays Desktop' processors or GPU's processors that can build up to hundreds of thousands threads.
If you use a semaphore, you are waking up to ask that thread after some time, and if the conditions has not being met, then you go to sleep again. This is usually in the ms time, but still, is some time that the processor dedicate to something useless.
MORE CLEAR
When you define a callback, is as if you were to sleep and you say to somebody.
Don't call me, I will call you when I'm ready to go.
Instead, using a semaphore is like:
Is ok if you call me 1000..0 times to ask me if I'm ready to go
The overheading in this case is obvious...you usually needs a time to wake up and to sleep back again. That's wasted processor time
Keep in mind that in the best case you are using a semaphore that put the Thread into sleep for a while, and then ask again if it is OK to proceed.
If this is not the case, then this thread is using ALL the processor cycles assigned to it to ask you if it's ok to proceed, that is 100% CPU Usage in that Thread evaluating just a condition.

Using main and background threads [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to have some clarify about it.
I know that I need to update UI in main thread. Are there anything else?
What I need to do in main thread and what in background threads?
In addition to UI updates, as a broader thread-safety strategy, people will often dispatch their model updates to the main thread as a simple synchronization technique, too.
Synchronization is ultimately the process of assuring that an object is in a logically consistent state, i.e. that while an object is being used on one thread, that it isn't simultaneously being mutated by some other thread. Traditionally, one might accomplish this by employing locks (e.g. NSLock, #synchronized, etc.) but you can also achieve this by dispatching all interaction with a particular object to a serial queue. While you can replace locks with a dedicated serial queue, in many cases it's just as easy to dispatch all updates to the object in the main queue. It turns out to be a convenient way to synchronize your model objects that might otherwise would have been used/mutated by separate threads.
For more information, see the Eliminating Lock-Based Code section in the Migrating Away from Threads chapter of the Concurrency Programming Guide.
It all depends.
Modern OS take advantage of the multiple cores or virtual CPUs, so when you run your app, the OS defines what to run where, and usually your program runs in multiple threads.
If there are data dependencies, then you should run things in specific threads, unless you run stuff in background or different threads, then you can implement notifications to ensure that the data you need is ready when you need it. You should also take into account the thread safe nature of the the different ways to define properties. So... other than the UI stuff in main, you can run anything pretty much wherever you want.

How to get the bandwidth of internet connection in IOS [duplicate]

This question already has an answer here:
iOS Detecting connection speed or type [duplicate]
(1 answer)
Closed 9 years ago.
Using Objective C, I need find the specific bandwidth of the network an iOS is connected to. Does anyone have any hints for finding this information with code?
You just need to place a file big enough in one of your servers (or somewhere else)
This could be done using a normal NSURLConnection.
Before start downloading, set up a timer. When your file's been downloaded, you stop the timer. Then you have the amount of time it took for the device to download X MB. You can do the calculations based on this. Once this has been done, the file can be discarded.
Of course, this will vary depending on how far the device is from the server, ping, routing, etc... but I think it's good enough for an implementation this simple. If you want more accuracy repeat the operation several times and calculate average speed.

Detail about nsrunloop [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Understanding NSRunLoop
Till now I know that every thread has its own runloop I don't know whether I'm thinking right.
I don't have a brief idea about nsrunloop but I'd like to know what is nsrunloop and what it's purpose and why all the thread has its own runloop(if I'm right). Please help me to let me know about nsrunloop.
a runloop is basically an extended while loop that works like C select call.
it is responsible for getting events from its sources and dispatching those.
mouse clicks, window moves, timers, stream events, ...... anything can be a runloop source.
thats the gist. read more about it in the countless dupes this has on SO or in the apple docs :)

Resources