ios - 从 NSOperation 子类读取数据到多个 View Controller

标签 ios nsoperation

我将解释场景。

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

现在,我想从 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];

最佳答案

你可以使用共享实例类,这是我一直做的:

数据库.h

#import <Foundation/Foundation.h>

@interface Database : NSObject

@property (nonatomic, readonly) NSArray* myTable;

+(Database*) sharedInstance;

@end

数据库.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;

关于ios - 从 NSOperation 子类读取数据到多个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24823889/

相关文章:

iphone - 如何优化 NSOperationQueue 启动线程的回调

iphone - 如何更改 UIImagePickerController 中的取消按钮标题?

ios - 将整数上传到 Firebase 数据库 IOS 的按钮

ios - UIBarbuttonItem 对 UIToolBar 的操作未被调用

iOS 应用程序在后台播放音乐时被终止

multithreading - 在主线程上运行NSOperation

ios - 我的应用程序崩溃了,并显示该类不符合使用swift编写的key tableView的键值编码

ios - NSOperationQueue 什么时候从队列中删除一个操作?

iphone - 带有 UIActivityIndi​​cator 的 UIAlertView 显示得太晚/线程问题

iphone - NSOperation仅在iOS 3设备上泄漏