1

我被这个问题困住了,看起来像多线程,但我对这类主题很陌生。我需要一些专家的帮助!!!

【问题条件】

  1. 需要调用一个有 3 个参数的方法,一个参数是@selector( myMethod: )

  2. 需要多次调用 (1)

  3. 需要确保 (1) 的每个选择器都已完成,以便继续下一步

  4. @selector( myMethod: )正在设置 xArray,一个对象 X 的数组以使其简单

  5. 所以,从逻辑上讲,我有一个带有多线程访问它的 xArray,并且需要以某种方式处理 xArray 的所有元素......

[想法]

performSelector 没有帮助,因为我需要输入的是带有 @selector 参数的方法...

[伪代码]

// The Starting Point of Alghorithm
- (void)initialCallerMethod {

  for(int i=0; i < [calendarArray count]; i++) {
    calendar = [calendarArray objectAtIndex:i];  
    // fetch the events feed
    NSString* alternateLink = [calendar alternateLink];
    NSURL* feedURL = [NSURL URLWithString:alternateLink];
    if (feedURL) {

      [self setEventFeed:nil];

      GDataQueryCalendar *query = [GDataQueryCalendar calendarQueryWithFeedURL:feedURL];
      [query setMaxResults:100]; 

      GDataServiceGoogleCalendar *service = [[[CalendarService alloc] init] calendarService];
      GDataServiceTicket *ticket;
      ticket = [service fetchFeedWithQuery:query 
        delegate:self
        didFinishSelector:@selector(calendarEventsTicket:finishedWithFeed:error:)];

      if ([self eventFetchError] == nil) {
          // query succeeded
 NSLog(@"Query succeeded");
 [self howToDoThis];    
      }
    }
  }
}

// @selector's method with 3 arguments
- (void)calendarEventsTicket:(GDataServiceTicket *)ticket
            finishedWithFeed:(GDataFeedCalendarEvent *)feed
                       error:(NSError *)error {

 [self setEventFeed:feed];
}

//
// Somewhere I want to do something like this
//
- (void) howToDoThis {
  GDataFeedCalendarEvent* feed = [self eventFeed];
  NSArray *entries = [feed entries];

  // for now, I get's zero...
  NSLog(@"FEED ENTRIES COUNT: %d", [entries count]);

  for (int idx = 0; idx < [entries count]; idx++) {
    // to make it simple, I'm just accumulating elements of array
    id elm = [entries objectAtIndex:idx];
    [anArrayToSumUp addObject: elm ]; 
   }
}

我真的满载而归...

请指教...

胜美

==== 一些进步,或挣扎... 2009/10/29

蒂姆,我为 NSInvocation 和 NSInvocationOperation 做了一些阅读。听起来很有用。现在,你知道如何传递“选择器的地址”了吗?你看,我可以使用 NSInvocation 设置目标、选择器和参数,但我如何传递 @selector(...) 的地址?

[使用 NSInvocation 之前] ticket = [service fetchFeedWithQuery:query delegate:self didFinishSelector:@selector(calendarEventsTicket:finishedWithFeed:error:)];

[尝试使用NSInvocation,除了将选择器作为参数传递之外,越来越接近]

retInvo = [NSInvocation invocationWithMethodSignature: [self methodSignatureForSelector:@selector(finishMethod:withArray:)]]; [retInvo setTarget:self];

// * 这不行 * [retInvo setSelector:@selector(finishMethod:withArray:)]; // 这不行

[retInvo setArgument:&calendar atIndex:2]; [retInvo setArgument:&events atIndex:3];

NSInvocationOperation* invoFinishOperation = [[NSInvocationOperation alloc] initWithInvocation:retInvo];

4

2 回答 2

0

为了处理您需要使用选择器作为参数调用方法,我会查看NSInvocation。NSInvocation 的实例本质上是一个转换为对象的方法调用 - 您指定一个目标和一系列参数,然后调用invoke该调用,它就会像直接的方法调用一样运行。

于 2009-10-28T15:28:31.607 回答
0

等待这些方法返回会让你的 UI 看起来被挂起。这就是为什么它们异步执行并给你一个回调。您作为所述回调提供的方法应该负责告诉您的应用程序的其余部分它已完成等待。

于 2009-10-28T15:43:52.903 回答