我正在實施我的第一個代碼https://github.com/ReactiveCocoa/ReactiveCocoa。爲什麼在ReactiveCocoa中調用信號兩次?
用於登錄用戶。行[subscriber sendNext:user];
被調用兩次,但我希望只有一個。和地圖是不是在所有所謂的(所以自動登錄不會被調用)
這是我實現:
-(RACSignal *) login:(NSString *)email pwd:(NSString *)pwd
{
DDLogInfo(@"Login user %@", email);
RACSignal *login = [RACSignal createSignal:^ RACDisposable *(id<RACSubscriber> subscriber)
{
[PFUser logInWithUsernameInBackground:email password:pwd block:^(PFUser *user, NSError *error) {
if (error) {
[subscriber sendError:error];
} else {
[subscriber sendNext:user];
[subscriber sendCompleted];
}
}];
return nil;
}];
[login map:^(PFUser *user) {
return [self autoLogin:user];
}];
return login;
}
正所謂是這樣的:
NSString *email = data[@"email"];
NSString *pwd = data[@"pwd"];
[SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];
RACSignal *login = [[SyncEngine server] login:email pwd:pwd];
[login
subscribeCompleted:^
{
[[NSNotificationCenter defaultCenter]
postNotificationName:NOTIFY_LOGIN_CHANGED
object:self];
[SVProgressHUD showSuccessWithStatus:LOC_OK];
[self cancelForm];
}];
[login
subscribeError:^(NSError *error)
{
[SVProgressHUD dismiss];
[AppUrls alertError:LOC_ERROR_LOGING msg:error.userInfo[@"error"]];
}];
單步執行代碼並查看發生了什麼。或者在進入sendNext時設置一個斷點,並在每次調用時檢查調用堆棧。 –