Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-07-2011, 08:57 AM   #1 (permalink)
Awesome
 
Esko2300's Avatar
 
Join Date: Jun 2009
Location: New York, N.Y.
Posts: 389
Esko2300 is on a distinguished road
Default NSInvocation help

Can someone tell me any good websites or books that explain the NSInvocation class, preferably ones with good examples about how to implement this class and situations why i would use them. Thanks
Esko2300 is offline   Reply With Quote
Old 01-07-2011, 09:38 AM   #2 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 864
nobre84 is on a distinguished road
Default

I use NSInvocation on situations where I need to perform a method on another thread, and that method takes a number of arguments, instead of just one. Normally you would need to wrap your arguments in some way, then unwrap when running the thread. With NSInvocation, you can prepare an invocation to the method you need regardless of its signature, and then invoke it on any thread.

Matt Gallager made a nice add-on category that makes the construction of an NSInvocation as simple as sending it the message itself, with the arguments you want.
Cocoa with Love: Construct an NSInvocation for any message, just by sending
nobre84 is offline   Reply With Quote
Old 01-07-2011, 10:04 AM   #3 (permalink)
Awesome
 
Esko2300's Avatar
 
Join Date: Jun 2009
Location: New York, N.Y.
Posts: 389
Esko2300 is on a distinguished road
Default

I thank you for your response but i have one more question. What i want to accomplish is this. I want the user to press a button on the screen and when the user does a rectangle with grow on the screen to a certian height. After that rectangle grows i want another and then another to grow on the screen and so forth. Would using NSinvocation be a good means about doing this? I am trying to teach myself about NSInvocation and this is the first thing that came to my mind on how to go about doing it since i want the frist rectangle to say hey im done to the second rectangle. Thanks
Esko2300 is offline   Reply With Quote
Old 01-07-2011, 11:01 AM   #4 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 864
nobre84 is on a distinguished road
Default

I don't think it would be necessary to use NSInvocation for that... You could just use animation blocks and set up a selector to be performed at the end which would start it again...
Begin an animation block, set self as the delegate, then respond to this method to handle the repetition:
Code:
-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
nobre84 is offline   Reply With Quote
Old 01-07-2011, 11:14 AM   #5 (permalink)
Awesome
 
Esko2300's Avatar
 
Join Date: Jun 2009
Location: New York, N.Y.
Posts: 389
Esko2300 is on a distinguished road
Default

YA but i have to use NSInvocation, i have to show a potential job i understand it and this is the only way i can think about using it and displaying my knowledge of it, is there any other suggestions you have in order for me to implement this feature?
Esko2300 is offline   Reply With Quote
Old 01-07-2011, 12:35 PM   #6 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 864
nobre84 is on a distinguished road
Default

Aside from my first example with threads, I know the NSUndoManager class capabilities are based on NSInvocation ...
The example you first thought wouldn't really be a good usage scenario for it I guess.
nobre84 is offline   Reply With Quote
Old 01-07-2011, 02:53 PM   #7 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Esko2300 View Post
YA but i have to use NSInvocation, i have to show a potential job i understand it and this is the only way i can think about using it and displaying my knowledge of it, is there any other suggestions you have in order for me to implement this feature?
NSInvocation lets you invoke a method that takes an arbitrary number of arguments, where each argument is an arbitrary type.

For instance, the normal timer methods let you send a message when the timer goes off, but that message includes one and only one parameter: the timer object. you can't use a timer to call some arbitrary method with non-object parameters. Instead, you have to use a timer to invoke a "glue" method that then invokes the other method.

If you create an NSInvocation, you can use the scheduledTimerWithTimeInterval:invocation:repeats: form of a timer, which calls the method described in the invocation object. The invocation is set up in advance with all the parameters and their types.

The trick with NSInvocation is to ask an object for it's method signature using code like this:

Code:
NSMethodSignature* theMethodSig = [someObject instanceMethodSignatureForSelector: someSelector];
That lets you cheat a little bit, and not worry about building your own method signature. You then just plug in the signature and parameters into the invocation.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 01-07-2011, 04:34 PM   #8 (permalink)
Awesome
 
Esko2300's Avatar
 
Join Date: Jun 2009
Location: New York, N.Y.
Posts: 389
Esko2300 is on a distinguished road
Default

Thanks for both your explanation's im starting to understand why i would exactly use NSInvocation in my programs. Another question though how would i go about using the [invocation setReturnType:&r] and [invocation getReturnType:&t]?

what im trying to do is grow a rectangle after a certian time has elapsed half a second to be exact and when i get to the height i want it will fire off another rectangle. and so on and so forth. But im getting the function to fire off but im not getting the return value of my function which is supposed to update the values of the rectangle. thisis how im doing it


//when the user presses a button
Code:
#pragma mark My Actions
-(IBAction)buttonPressed:(UIButton*)sender{
	rect1 = CGRectMake(rectXPos, rectYPos, rectWidth, rectHeight);	
	view1 = [[UIView alloc]initWithFrame:rect1];
	[view1 setBackgroundColor:[UIColor redColor]];
	[self.view addSubview:view1];	

	signature = [self.class instanceMethodSignatureForSelector:@selector(growRectangle:withRect:)];
	invocation = [NSInvocation invocationWithMethodSignature:signature];
	[invocation setTarget:self];
	[invocation setSelector:@selector(growRectangle:withRect:)];
	[invocation setArgument:&view1 atIndex:2];
	[invocation setArgument:&rect1 atIndex:3];
	[invocation setReturnValue:&rect1];
	
	[NSTimer scheduledTimerWithTimeInterval:.5 invocation:invocation repeats:TRUE];
}

//and this is the function
Code:
#pragma mark My Functions
-(CGRect)growRectangle:(UIView*)v withRect:(CGRect)r{
	
	if(r.size.height>-180){
		r.size.height-=20;
		v.frame=r;
		//would i use getReturnValue here? when i do nothing changes in //my rect1
                [invocation getReturnValue:&r];

	}
	else{
		rect2 = CGRectMake(rectXPos+50, rectYPos, rectWidth, rectHeight);
		view2 = [[UIView alloc]initWithFrame:rect2];
		[view2 setBackgroundColor:[UIColor greenColor]];
		[self.view addSubview:view2];
		
		[invocation setSelector:@selector(growRectangle2:withRect:)];
		[invocation setArgument:&view2 atIndex:2];
		[invocation setArgument:&rect2 atIndex:3];
		
	}
	return r;
}
Esko2300 is offline   Reply With Quote
Old 01-07-2011, 06:28 PM   #9 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Esko2300 View Post
Thanks for both your explanation's im starting to understand why i would exactly use NSInvocation in my programs. Another question though how would i go about using the [invocation setReturnType:&r] and [invocation getReturnType:&t]?

You should not need to use setReturnType, because the two statements

instanceMethodSignatureForSelector
and
invocationWithMethodSignature:
should get everything they need to know about the method (number and types of parameters, type of method return type, if any). You pass that method signature to the method invocationWithMethodSignature to create your invocation object.

As to how to get the return type: Why do you need it? It seems to me that if you're firing off a method using a timer, the return value will simply be ignored. You're not calling the method in the normal way, where you can evaluate the result after the method call and decide what to do next. The timer will fire off the method that's described in the invocation, and then queue up another call (if the timer is a repeating timer.)

I am no expert on invocations, mind. The only time I could think of that you would use the result of a method you invoked with an NSInvocation is if you invoked the method synchronously and waited around for the result to be returned to you.

I just looked at your growRectangle method. It looks to me like you are missing the point of NSInvocation objects completely.

An NSInvocation lets you turn a method call and it's parameters into an object. You can then use the invocation object to invoke that method at a later time.

The invocation object is used to call a method, but in the called method, everything seems normal. You're not aware that there was an NSInvocation object involved. Your method got called with it's normal parameters, and returns whatever value it normally returns. You don't have access to the invocation object inside your called method, and have no reason to want to.

Thus, your growRectangle method should not make any references to an invocation object. You won't have a pointer to it, and should not have a pointer to it. It may even be deallocated by the time your growRectangle method gets called.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-13-2011, 04:58 AM   #10 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 2
xerxes235 is on a distinguished road
Default Step by Step Tutorial for NSInvocation: The Compelling Techniques in Iphone Developme

Hamidreza Vakilian | NSInvocation Tutorial Part2: Compelling Techniques
xerxes235 is offline   Reply With Quote
Reply

Bookmarks

Tags
nsinvocation, queue, threading, threads

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 360
7 members and 353 guests
blueorb, fredidf, iAppDeveloper, iGamesDev, mottdog, sacha1996, Touchmint
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:32 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0