开发者

asynchronous call using the NSOperationQueue

I am a beginner in developing iPhone applications. I was doing this sample program below and got an error- invalid use of void expression

threadsss.h
------------
#import <Foundation/Foundation.h>

@interface threadsss : NSObject {

    BOOL m_bRunThread;
    int a,b,c;

}
-(void)Thread;
-(void)add;
-(void)display;
@end

threadsss.m
------------

#import "threadsss.h"


@implementation threadsss
-(void)Thread
{
    m_bRunThread = YES;
    NSOperationQueue* queue = [[NSOperationQueue alloc]init];
    NSInvocationOperation* operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(display) object:nil];
    [operation addDependency:[self add]];
    [queue addOperation:operation];
    [queue release];
}
-(void)add
{
    NSLog(@"Going to add a and b!!");
    a=1;
    b=2;
    c = a + b;
    NSLog(@"Finished adding!!");
}
-(void)display
{
    NSLog(@"Into the display meth开发者_如何学Cod");
    NSLog(@"The value od c is:%d",c);
}
@end

main.m
-------
#import <Foundation/Foundation.h>
#import "threadsss.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    threadsss* thread = [[threadss alloc]init];
    [thread Thread];

    [pool drain];
    return 0;
}

I want to make an asynchronous call between the add and the display methods.After calling the display method i want to execute the add method. and in the meanwhile after printing the "I'm into the display method" the display method will wait for the add to perform its operation and the add after doing its operation will notify its completion to the display method.The display method will then print the result c.

I have tried to implement it with that in my mind.Do i need to do any other modification in my program or is the way i have implemented through dependecies is correct.

EDITED

threadss.h
-----------

#import <Foundation/Foundation.h>


@interface threadss : NSObject {

    BOOL m_bRunThread;
    int a,b,c;
    NSOperationQueue* queue;
    NSInvocationOperation* operation;
    NSInvocationOperation* operation1;
    NSConditionLock* theConditionLock;

}
-(void)Thread;
-(void)add;
-(void)display;
@end


threadss.m
-----------

#import "threadss.h"


@implementation threadss

-(id)init
{
    if (self = [super init]) {
     queue = [[NSOperationQueue alloc]init];
     operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(display) object:nil];
     operation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(add) object:nil];
     theConditionLock = [[NSConditionLock alloc]init];
    }
    return self;
}
-(void)Thread
{
    m_bRunThread = YES;

    //[operation addDependency:operation1];
    if (m_bRunThread) {
    [queue addOperation:operation];
    }
    //[operation addDependency:operation1];
    [queue addOperation:operation1];

    //[self performSelectorOnMainThread:@selector(display) withObject:nil waitUntilDone:YES];
    //NSLog(@"I'm going to do the asynchronous communication btwn the threads!!");
    //[self add];
    //[operation addDependency:self];
    sleep(1);
    [queue release];
    [operation release];
    //[operation1 release];
}
-(void)add
{
    NSLog(@"Going to add a and b!!");
    a=1;
    b=2;
    c = a + b;
    NSLog(@"Finished adding!!");
}
-(void)display
{
    NSLog(@"Into the display method");
    [operation1 waitUntilFinished];
    NSLog(@"The Result is:%d",c);

}
@end

main.m
------

#import <Foundation/Foundation.h>
#import "threadss.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    threadss* thread = [[threadss alloc]init];
    [thread Thread];

    [pool drain];
    return 0;
}

I made two operation queues.

But using waitUntilFinished on the same queue may lead to deadlock.How do i do the wait in display method for the add operation to complete its execution.


First, it would be easier to answer your question if you identified the line than the compiler was complaining about and showing only the relevant code. However, in this case it's pretty straight-forward. It's this line:

[operation addDependency:[self add]];

The add method returns nothing (void). And you're telling operation to add that as a dependency. What does that mean?

Either, you should change add to return self or call add on the line before:

[self add];
[operation addDependency:self];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜