将NSOperation子类中的数据读取到多个viewcontrollers

时间:2014-07-18 11:23:45

标签: ios nsoperation

我将解释情景。

我有一个NSOperation子类。在这堂课中,我正在阅读多个蓝牙设备的数据。 我在NSOperation A中创建ViewController类的对象,并使用delegate子类中的NSoperation方法获取数据。

现在,我想从Viewcontroller B读取数据而不创建NSoperation的对象。

请检查我的NSOperation子类

NOPerationSubclass.h     `

@protocol NOPerationSubclassDelegate`;

@interface NOPerationSubclass : NSOperation{

BOOL executing;
BOOL finished;
}


@property id<NOPerationSubclassDelegate> delegate;

 - (id)initWithConnectDevice:(ConnectDevice *)cDevice toPeripheral:(CBPeripheral *)peripheral;

@end
@protocol NOPerationSubclassDelegate

-(void)updateUIFromOperation:(NOPerationSubclass *)operation;
@end

NOPerationSubclass.m

- (id)initWithConnectDevice:(ConnectDevice *)cDevice toPeripheral:(CBPeripheral *)peripheral{

if (self = [super init]) {


    executing = NO;
    finished = NO;
    self.connectDevice = cDevice;
    [self.connectDevice setDelegate:self];
    self.connectedPeripheral = peripheral;

    dataDic = [[NSMutableDictionary alloc] init];


}
return self;
}

 -(BOOL)isConcurrent{
   return YES;
}
- (BOOL)isExecuting {
  return executing;
 }

 - (BOOL)isFinished {
   return finished;
 }

 -(void) terminateOperation {


[self willChangeValueForKey:@"isFinished"];
[self willChangeValueForKey:@"isExecuting"];
finished = YES;
executing = NO;
[self didChangeValueForKey:@"isExecuting"];
[self didChangeValueForKey:@"isFinished"];
}

- (void)start {

@autoreleasepool {

    if (self.isCancelled){
        [self willChangeValueForKey:@"isFinished"];
        finished = YES;
        [self didChangeValueForKey:@"isFinished"];
        return;
    }
     timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self    selector:@selector(timerFired:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] run];

}

-(void)timerFired:(id)sender{

if (self.isCancelled){

    [self willChangeValueForKey:@"isFinished"];
    finished = YES;
    [self didChangeValueForKey:@"isFinished"];
    return;
 }

[connectDevice calldiscoverServicesForPeripheral:connectedPeripheral];



}


 -(void)getDataFromPeripheral:(CBPeripheral *)peripheral Data:(NSString *)data{

[dataDic setValue:[peripheral.identifier UUIDString] forKey:@"identifier"];
[dataDic setValue:data forKey:@"data"];

[[[AppDelegate app] devicesDataArray] addObject:dataDic];


[(NSObject *)self.delegate performSelectorOnMainThread:@selector(updateUIFromOperation:) withObject:dataDic waitUntilDone:NO];

NSLog(@"PERIPHERAL DATA::+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%@",peripheral.name);
 }

而且,我从ViewController A这样调用这个NSOpeartion类

  NOPerationSubclass *queue = [[NOPerationSubclass alloc] initWithConnectDevice:connectDevices   toPeripheral:peripheral];
  queue.delegate = self;

  [[[AppDelegate app] mainOperationQueue] addOperation:queue];

1 个答案:

答案 0 :(得分:2)

您可以使用共享实例类,这是我经常做的事情:

<强> Database.h

#import <Foundation/Foundation.h>

@interface Database : NSObject

@property (nonatomic, readonly) NSArray* myTable;

+(Database*) sharedInstance;

@end

<强> Database.m

#import "Database.h"

@implementation Database

Database* _db = nil;
+(Database*) sharedInstance {

    if (!_db)
        _db = [[Database alloc] init];

    return _db;

}

-(id) init {

    self = [super init];

    // Do loading here
    return self;

}

@end

然后,只要您想访问数据:

[Database sharedInstance].myTable;