所以我试图在DBManager
对象中分离我的Parse调用。这是我的一个电话:
- (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 :(得分: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];
}];