-2

I'm new to ios development and have been reading up the docs on the apple website, where I stumbled upon NSTask, as well as NSOperation. I'm unable to tell the difference between the two in terms of what they actually do. Could someone please list out what the difference between them is and when it is appropriate to use each of them?

Edit: What I don't understand is how they are different from each other. The docs say an NSTask object is used to run a subprocess and can only be run once. They also say that an NSOperation encapsulates a task that can only be run once.

They seem to do the exact same thing. That's why I want to know the pros/cons of using each of them.

sosale151
  • 360
  • 2
  • 4
  • 19
  • 1
    See this [answer](http://stackoverflow.com/questions/1124207/what-is-the-basic-difference-between-nstimer-nstask-nsthread-and-nsrunloop?answertab=active#tab-top). – Vivek Molkar May 29 '15 at 05:53
  • 1
    Please be more specific about what you do and don't understand. They have essentially nothing to do with each other. – jscs May 29 '15 at 05:55
  • Edited question @JoshCaswell – sosale151 May 29 '15 at 06:00
  • @VivekMolkar, So from what I understand, NSTasks are more specialized, not available on the iPhone, and do not result in the crashing of the program when the NSTask crashes, and they're generally on different threads. NSOperation on the other hand is more for general routine tasks, they can crash the program and they can but don't HAVE to be on different threads. Is that a correct understanding? – sosale151 May 29 '15 at 06:06
  • If someone downvotes it, please give a reason for it. This is a legit question, and not a duplicate of anything else that exists on the site. If the question is not clear, indicate it and I'll make edits to clarify any details. – sosale151 May 29 '15 at 07:45

1 Answers1

3

A NSTask as you've read is used to run a new child process. A process is a new entity with a separated memory space. A process can create multiple threads that will run in its memory space. NSTask is not available in iOS since an app cannot have more than one process (which is the app itself).

A simplified explanation of NSOperation would be that it's just like creating a new thread. I think what's confusing for you is the use of the word "task" in the documentation of NSOperation. They are not talking about a NSTask, but rather about the code that an operation is executing (i.e. your "task").

There's not much point in trying to compare the two or spot the differences since they are not really related. Not to mention the fact that you can't really use NSTask or process concepts in iOS...

Artal
  • 8,933
  • 2
  • 27
  • 30
  • You were right in that the use of the word 'task' in the NSOperation documentation really confused me. Thanks for the help! But damn, stack overflow can be harsh sometimes. – sosale151 May 31 '15 at 15:53