Jack Gleeman
Jack Gleeman

Reputation: 593

Keeping a class running in the background when another has been started

I will try and explain this as best as possible if I have this code here

ViewTwoController *home = [[ViewTwoController alloc] initWithNibName:@"contentscreen" bundle:nil];
[self presentModalViewController:home animated:YES];
[home release];

I will start a new .m and .h class. What I would like to try and do however is when this is called, have the .m and .h class where it was called from running in the background so I do not lose data.

The best example I can think of is with Android. If you begin a new class, and don't add the finish() statement in the class the call was made from, the previous class runs behind the current class (that was pushed to the front) and maintains all the data it originally had, so if you hit a return button, you will see the information you had moments ago. Is this possible? I can try add more detail if people cannot understand what I am trying to do.

Upvotes: 1

Views: 78

Answers (4)

Grady Player
Grady Player

Reputation: 14549

You need to understand a objects life cycle a little better.

An object is brought into existence generally with a 2 part process.

  1. allocation - (grabbing the memory for the object and its members)
  2. initialization - (setting the object up for use)

This can be combined into single step with the +new class method which combines alloc and init.

lets introduce an example class called MyClass and an object of that class called myObject. By convention classes start with uppercase letters and objects start with lowercase letters. So Without further ado, some code:

MyClass * myObject;

this this makes an object pointer, but doesn't allocate any memory for it or direct the pointer to reference anything.

myObject = [[MyClass alloc] init];

this actually creates an instance of MyClass, passes the -init message to it, then assigns the return value of the init message to myObject. At this point the reference count of this object is 1.

myObject can potentially go out of scope but that alone doesn't free the memory that was allocated during the alloc step.

in order to free that memory a release message will need to be passed to the object.

[myObject release];

The effect of release is to decrement the reference count, if the reference count is already 1 then the object will then be passed the -dealloc indicating that it is actually being freed.

back to your question... essentially [self presentModalViewController:home animated:YES]; ends up calling -retain on home, so that it will not be destroyed until you dismiss the modal view controller. In affect when you call release or autorelease you aren't dealloc'ing the object, just telling the object:
"Hey, I don't need you anymore, and if no one else does either then free up all the memory that you grabbed earlier".

Upvotes: 1

sach
sach

Reputation: 1069

In this case you are presenting new view controller. The main thread will be in the new controller presented. If you want something to run in background in the previous view controller then you can create a background thread. This can be done using [self perfomselectorInThebackground ... ] Or some other methods like GCD. (The main thing is you should not block Main thread)

Upvotes: 0

mjisrawi
mjisrawi

Reputation: 7936

When you present a modal view controller, its parent (the view controller you presented it from) isn't destroyed (unless you specifically release it, which would probably crash your app later). So if you're wondering whether its still in memory; it is. As for tasks still running, it depends on what those tasks are. For example, you can still send it messages (call methods) and it will gladly receive those messages from your or from a delegate and perform whatever action it has to while it's off-screen.

Hope that helped.

Upvotes: 0

alinoz
alinoz

Reputation: 2822

Your problem has nothing to do with "class running in the background" but more with how you manage your data.

Upvotes: 0

Related Questions