RACSignal:从不调用信号

时间:2016-11-08 20:36:40

标签: objective-c racsignal

我有以下代码,由另一个信号调用:

- (RACSignal *)uploadFormItemAttachment_SO:(MyManagedAttachment *)attachment attachmentHeader:(MyFormItemAttachmentHeader*) attachmentHeader
{

  RACSignal* signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {

    // Code omitted for brevity
    // NSDictionary* parameters = @{ @"parameter1" : param1_value};


    AFHTTPRequestOperation* operation = [self.requestOperationManager POST:@"UploadForm" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
      [formData appendPartWithFileURL:fileURL
                                 name:@"content"
                                error:nil];

    } success:^(AFHTTPRequestOperation *operation, id responseObject) {

      [self logRequest:operation response:responseObject];

      BOOL success = [responseObject[@"success"] boolValue];
      if (success)
      {
        attachment.status = @(AttachmentStatusSent);
        [attachment.managedObjectContext MR_saveToPersistentStoreAndWait];
        [subscriber sendNext:@(success)];
        [subscriber sendCompleted];
      }
      else
      {
        NSDictionary* userInfo = nil;
        NSString* message = responseObject[@"message"];
        if (message != nil && ![message isEqual:[NSNull null]])
        {
          userInfo = @{ NSLocalizedDescriptionKey : message };
        }
        else
        {
          userInfo = @{ NSLocalizedDescriptionKey : [NSString stringWithFormat:@"Error uploading image: %@", filename]};
        }
        NSError* error = [NSError errorWithDomain:SEFSAPIClientErrorDomain code:1100 userInfo:userInfo];
        [subscriber sendError:error];
      }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
      [self logRequest:operation error:error];
      [subscriber sendError:error];
    }];

    [self.requestOperationManager.operationQueue addOperation:operation];

    return [RACDisposable disposableWithBlock:^{
      [operation cancel];
    }];


  }];

  return signal;
}

我的问题是从不调用信号对象,代码永远不会运行。它直接进入最后一个陈述

return signal;

并且信号内的代码永远不会被执行。有人可以帮忙吗?

我尝试在RACSignal定义的末尾添加[subscriber sendNext:signal],但没有区别。

以下方法调用上一个方法:

- (RACSignal *)uploadPendingAttachments_SO
{
  NSArray *pendingAttachments = [MyManagedAttachment MR_findAllWithPredicate:[NSPredicate predicateWithFormat:@"status == %ld && form.status == %ld", MyAttachmentStatusPending, SEFSFormStatusUploadedForm]
                                                                     inContext:[NSManagedObjectContext MR_defaultContext]];

  RACSignal* batchSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    [pendingAttachments enumerateObjectsUsingBlock:^(MyManagedAttachment* pendingImage, NSUInteger idx, BOOL *stop) {

      // Check if we have a top list item id which indicates form data has been created on the back-end SharePoint server.

      NSString* siteID = pendingImage.siteID;
      NSString* formID = pendingImage.formID;

      NSManagedObjectContext* context = [NSManagedObjectContext MR_contextWithParent:[NSManagedObjectContext MR_defaultContext]];
      NSPredicate* predicate = [NSPredicate predicateWithFormat:@"projectID = %@ AND formID = %@", siteID, formID];


      MyManagedForm* form = [MyManagedForm MR_findFirstWithPredicate:predicate
                                                               inContext:context];

      if (form.topListItemID && pendingImage.formItemID) {

        NSString* listName = pendingImage.list;
        RACSignal* getFormItemAttachmentHeadersSignal = [[self getFormItemAttachmentHeaders:listName
                                                                              topListItemID:form.topListItemID
                                                                                       form:form
                                                          ] map:^id(NSMutableArray* value) {


          NSArray* attachmentHeaders = [value copy];

          RACSignal* attachmentsBatchSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            [attachmentHeaders enumerateObjectsUsingBlock:^(MyFormItemAttachmentHeader* attachmentHeader, NSUInteger idx, BOOL *stop)
             {
               // Look for the local attachment using attachment header from server

               NSPredicate* predicate = [NSPredicate predicateWithFormat:@"identifier = %@", attachmentHeader.document];
               NSArray* foundAttachment = [pendingAttachments filteredArrayUsingPredicate:predicate];

               MyManagedAttachment* fullAttachment = foundAttachment[0];

               NSString* listName = pendingImage.list;
               RACSignal* uploadFormItemAttachmentSignal = [[self uploadFormItemAttachment_SO:fullAttachment
                                                                          attachmentHeader:attachmentHeader                                                            ] map:^id(id value) {

                 BOOL success = [value boolValue];
                 return nil;
               }];


               [subscriber sendNext:uploadFormItemAttachmentSignal];

             }];

            [subscriber sendCompleted];
            return nil;
          }];

          return attachmentsBatchSignal; // check this

        }];

        [subscriber sendNext:getFormItemAttachmentHeadersSignal];

      }
      else {

        RACSignal* uploadFormSignal = [[self uploadAttachment:pendingImage] map:^id(NSNumber* value) {
          NSMutableArray* valuesArray = [NSMutableArray array];
          [valuesArray addObject:value];
          [valuesArray addObject:pendingImage.objectID];
          RACTuple* tuple = [RACTuple tupleWithObjectsFromArray:valuesArray
                                             convertNullsToNils:YES];
          return tuple;
        }];

        [subscriber sendNext:uploadFormSignal];
      }
    }];
    [subscriber sendCompleted];
    return nil;
  }];


  return [batchSignal flatten:2];
}

0 个答案:

没有答案