Я новичок в разработке приложений для iPhone.
Я делал эту программу ниже и получил ошибка - недопустимое использование пустого выражения
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 method");
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;
}
Я хочу сделать асинхронный вызов между методами add и display. После вызова метода display я хочу выполнить метод add. и в то же время после печати «Я в методе отображения» метод отображения будет ожидать, пока добавление выполнит свою операцию, а дополнение после выполнения своей операции сообщит о своем завершении методу отображения. Затем метод отображения напечатает результат c.
Я пытался воплотить это в своих мыслях. Нужно ли делать какие-либо другие изменения в моей программе, или это правильный способ, который я реализовал с помощью зависимостей.
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;
}
Я сделал две очереди операций.
Но использование waitUntilFinished в той же очереди может привести к тупиковой ситуации. Как мне выполнить метод ожидания в отображении для операции добавления, чтобы завершить ее выполнение.