2014-02-06 69 views
0

所以我試圖分開我的解析調用DBManager對象。這是我的一個電話:通過傳遞塊分隔DB邏輯

- (void)createMessageObject:(NSString*)text forConversation:(PFObject*)conversationObject withBlock:(PFBooleanResultBlock)block { 

    ///////////////////////////////// 
    // First create the object 
    PFObject* messageObject = [PFObject objectWithClassName:kWSMessageClassKey]; 
    [messageObject addObject:[PFUser currentUser] forKey:kWSMessageUserKey]; 
    [messageObject addObject:text forKey:kWSMessageTextKey]; 
    [messageObject addObject:[[WSWordlistManager shared] languageTarget] forKey:KWSMessageLanguageKey]; 


    [messageObject saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
     if (succeeded) { 

      /////////////////////////////// 
      // Now add it to the Conversation object 
      [conversationObject addObject:messageObject forKey:kWSConversationMessagesKey]; 
      [conversationObject saveInBackgroundWithBlock:block]; 

     } 
    }]; 
} 

現在我正在開發應用內聊天/消息功能。所以在上面的調用中,我想創建一個新的Message對象,如果它成功保存,則將其添加到現有的Conversation對象。問題在於我在我的ViewController中的原始呼叫:

[self createMessageObject:text forConversation:self.conversation withBlock:^(BOOL succeeded, NSError *error) { 

     if (succeeded) { 

      // PROBLEM HERE: HOW TO PASS BACK THE messageObject I CREATED IN THE DB CALL? 
      [self.objects addObject:messageObject]; 

      [self.messages addObject:[[JSMessage alloc] initWithText:text sender:sender date:date]]; 
      [self finishSend];     
      [self sendMessagePushFromUser:[PFUser currentUser] toUsers:self.friends messageObject:messageObject];     
      [self scrollToBottomAnimated:YES]; 

     } 

    }]; 

所以問題出在我評論過的行上。我需要訪問我在Parse調用中創建的messageObject,但是如何將其傳回?

+0

檢查此鏈接http://stackoverflow.com/questions/21613642/how-do-i-change-objects-value -passed作爲一種參數-內部塊 – Pawan

回答

0

您可以使用回調。事情是這樣的:

- (void)createMessageObject:(NSString*)text forConversation:(PFObject*)conversationObject withCallback:(void(^)(PFObject *messageObject))callback { 
    ///////////////////////////////// 
    // First create the object 
    PFObject* messageObject = [PFObject objectWithClassName:kWSMessageClassKey]; 
    [messageObject addObject:[PFUser currentUser] forKey:kWSMessageUserKey]; 
    [messageObject addObject:text forKey:kWSMessageTextKey]; 
    [messageObject addObject:[[WSWordlistManager shared] languageTarget] forKey:KWSMessageLanguageKey]; 


    [messageObject saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
     if (succeeded) { 
      if (callback) { 
       callback(messageObject); 
      } 

     } 
    }]; 
} 

然後調用它像這樣:

[self createMessageObject:text forConversation:self.conversation withCallback:^(PFObject *messageObject) { 
    NSLog(@"Text from message object: %@", [messageObject objectForKey:kWSMessageTextKey]); 

    [self.objects addObject:messageObject]; 

    [self.messages addObject:[[JSMessage alloc] initWithText:text sender:sender date:date]]; 
    [self finishSend]; 
    [self sendMessagePushFromUser:[PFUser currentUser] toUsers:self.friends messageObject:messageObject]; 
    [self scrollToBottomAnimated:YES]; 
}];