简体   繁体   English

从另一个类Objective-C调用方法

[英]Calling method from another class objective-c

So far the remote wipe works, but I'm having trouble starting a backup in the background. 到目前为止,远程擦除工作正常,但是我无法在后台启动备份。 The remote wipe also works in the background. 远程擦除也可以在后台运行。 I am trying to call the startBackgroundBackupActivity method from my locationHandler class which works in the background. 我试图从我的在后台运行的locationHandler类调用startBackgroundBackupActivity方法。

BackgroundBackupHandler.m

- (void) OnSyncComplete:(NSNumber*)result message:(NSString *)message{
    NSLog(@"-(void)OnSyncComplete:%@ message=%@",result, message);
    //jxxtodo: Ensure all existing objects are reset, including DB and network connections
    if (0 == [result intValue]){
        NSString *strMsg = [ErrorHandler getErrorTextByErrorNumber:SYNC_SUCCESS_INF withObjects:nil];
        [self logEvent:strMsg];
    }else if (2 == [result intValue]){//no sync required
        NSString *strMsg = [ErrorHandler getErrorTextByErrorNumber:NO_NEED_SYNC_INF withObjects:nil];
        [self logEvent:strMsg];
    }else if (3 == [result intValue]) {
        NSString *strMsg = [ErrorHandler getErrorTextByErrorNumber:SYNC_RESET_EMPTY_INF withObjects:nil];
        [self logEvent:strMsg];
    } else{
        NSString *strMsg = [ErrorHandler getErrorTextByErrorNumber:SYNC_COMMON_ERR withObjects:nil];
        [self logEvent:strMsg];

        return;
    }
    [m_pSyncController release];
    m_pSyncController = nil;
    self.m_backupSet = nil;
    [self performSelector:@selector(startBackgroundBackupActivity) withObject:nil afterDelay:1.5];
}

Right now, OnSyncComplete:message: is what calls startBackupActcitivy from within the BackgroundBackupHandler class. 现在, OnSyncComplete:message:是从BackgroundBackupHandler类内部调用startBackupActcitivy

I have another class LocationHandler which checks the flag sent from the server and does something based on the flag. 我有另一个类LocationHandler ,它检查从服务器发送的标志并根据该标志执行某些操作。 So if the flag is set to backup then wipe, it will run a backup then wipe the device. 因此,如果将该标志设置为“备份然后擦除”,它将先执行备份,然后擦除设备。

How would I call OnSyncComplete:message: from the LocationHandler class. 我将如何从LocationHandler类中调用OnSyncComplete:message:

Ive tried: 我试过了:

BackgroundBackupHandler *bgBackup = [[BackgroundBackupHandler alloc]init];
[bgBackup OnSyncComplete:[NSNumber numberWithInt:3] message:nil];

This is giving me errors and terminating my application. 这给了我错误,并终止了我的申请。 Is there anyway to call startBackgroundBackupActivity from the LocationHandlerClass . 无论如何, startBackgroundBackupActivityLocationHandlerClass调用startBackgroundBackupActivity

The error is that the application crashed and aborts. 错误是应用程序崩溃并中止。 NSInvalidArgumentException, where nil is not a legal NSManagedObjectContext. NSInvalidArgumentException,其中nil不是合法的NSManagedObjectContext。 The locationHandler will start a background task, which will then sync the device and return control to the OnSyncComplete method in the LocationHandler class which then in turn calls the OnSyncComplete in the BackgroundBackupHandler class. locationHandler将启动一个后台任务,该任务随后将同步设备并将控制权返回给LocationHandler类中的OnSyncComplete方法,然后该方法又调用BackgroundBackupHandler类中的OnSyncComplete。

The 2 ways to create communication between object are: 1) delegations 2) notifications 创建对象之间通信的2种方法是:1)委托2)通知

In your case it seems like the notification can work better. 在您的情况下,通知似乎可以更好地工作。 Check this out 看一下这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM