Ios 完成区块内操作完成后未获得结果

Ios 完成区块内操作完成后未获得结果,ios,Ios,这是我的解析器类,在解析了json url的对象数组之后,这个类工作得非常好 @interface parseOperation : NSOperation @property (nonatomic, copy) void (^errorHandler)(NSError *error); @property (nonatomic, strong, readonly) NSMutableArray *appRecordList; - (id)initWithData:(NSData *)dat

这是我的解析器类,在解析了json url的对象数组之后,这个类工作得非常好

@interface parseOperation : NSOperation
@property (nonatomic, copy) void (^errorHandler)(NSError *error);

@property (nonatomic, strong, readonly) NSMutableArray *appRecordList;

- (id)initWithData:(NSData *)data;
@end

#import "parseOperation.h"

@interface parseOperation () <NSURLConnectionDataDelegate>
{
NSMutableArray *array;
NSMutableData *mdata;
NSURLConnection *conn;

//  NSMutableArray *arrone;

}
@property (nonatomic, strong) NSMutableArray *appRecordList;
@property (nonatomic, strong) NSData *dataToParse;
@property (nonatomic, strong) NSMutableArray *workingArray;
@property (nonatomic, strong) Jobs *workingEntry;
@property (nonatomic, strong) NSMutableString *workingPropertyString;
@property (nonatomic, strong) NSArray *elementsToParse;
@property (nonatomic, readwrite) BOOL storingCharacterData;
@end

@implementation parseOperation
- (id)initWithData:(NSData *)data
{
self = [super init];
if (self != nil)
{
    _dataToParse = data;

}
return self;
}
- (void)handleError:(NSError *)error
{
    NSString *errorMessage = [error localizedDescription];
   UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Cannot Show Top Paid Apps.........."
                                                          message:errorMessage
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
[alertView show];
}

- (void)main
{
    //self.workingArray = [NSMutableArray array];
    self.workingPropertyString = [NSMutableString string];
    self.workingArray=[[NSMutableArray alloc]init];

NSDictionary *allData=[NSJSONSerialization JSONObjectWithData:_dataToParse options:0 error:nil];

for (NSDictionary *dict in allData)
{

    self.workingEntry =[[Jobs alloc]init];
    self.workingEntry.title=[dict objectForKey:@"title"];

   [self.workingArray addObject:self.workingEntry];


    self.workingEntry=nil;

}
    if (![self isCancelled])
{

    self.appRecordList = [NSMutableArray arrayWithArray:self.workingArray];

    NSLog(@"self.appRecordList======== %@", self.appRecordList);
    NSLog(@"parse complete......");

}

   // self.workingArray = nil;
self.workingPropertyString = nil;
self.dataToParse = nil;
}


@end
@接口解析操作:NSOperation
@属性(非原子,副本)无效(^errorHandler)(n错误*错误);
@属性(非原子、强、只读)NSMutableArray*appRecordList;
-(id)initWithData:(NSData*)数据;
@结束
#导入“parseOperation.h”
@接口解析操作()
{
NSMutableArray*数组;
NSMutableData*mdata;
NSURL连接*conn;
//NSMutableArray*arrone;
}
@属性(非原子,强)NSMutableArray*appRecordList;
@属性(非原子,强)NSData*dataToParse;
@属性(非原子,强)NSMUTABLEARRY*workingArray;
@财产(非原子、强)工作*工作中心;
@属性(非原子,强)NSMutableString*workingPropertyString;
@属性(非原子,强)NSArray*elementsToParse;
@属性(非原子,读写)BOOL存储CharacterData;
@结束
@实现解析操作
-(id)initWithData:(NSData*)数据
{
self=[super init];
if(self!=nil)
{
_dataToParse=数据;
}
回归自我;
}
-(无效)句柄错误:(n错误*)错误
{
NSString*errorMessage=[error localizedDescription];
UIAlertView*alertView=[[UIAlertView alloc]initWithTitle:@“无法显示顶级付费应用…………”
消息:errorMessage
代表:无
取消按钮:@“确定”
其他按钮:无];
[警报视图显示];
}
-(无效)主要
{
//self.workingArray=[NSMutableArray];
self.workingPropertyString=[NSMutableString];
self.workingArray=[[NSMutableArray alloc]init];
NSDictionary*allData=[NSJSONSerialization JSONObjectWithData:_dataToParse选项:0错误:nil];
用于(NSDictionary*所有数据中的dict)
{
self.workingEntry=[[Jobs alloc]init];
self.workingEntry.title=[dict objectForKey:@“title]”;
[self.workingArray addObject:self.workingEntry];
自工作中心=零;
}
如果(![自我取消])
{
self.appRecordList=[NSMutableArray数组WithArray:self.workingArray];
NSLog(@“self.appRecordList====%@”,self.appRecordList);
NSLog(@“解析完成…”);
}
//自工作阵列=零;
self.workingPropertyString=nil;
self.dataToParse=nil;
}
@结束
在这里,我得到了解析后的结果,self.appRecordList数组显示了正确的结果,但我的问题是,当我试图从具有null值的appdelegate访问数组时

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UINavigationController *nav;

}

@property (strong, nonatomic) UIWindow *window;

@property (nonatomic, strong) NSMutableArray *entries;

@end



#import "AppDelegate.h"
static NSString *const TopPaidAppsFeed =@"http:url";


@interface AppDelegate ()
@property (nonatomic, strong) NSOperationQueue *queue;
@property (nonatomic, strong) NSOperationQueue *queue1;
@property (nonatomic, strong) NSURLConnection *appListFeedConnection;
@property (nonatomic, strong) NSMutableData *appListData;

@end



@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURLRequest *urlRequest   = [NSURLRequest requestWithURL:[NSURL URLWithString:TopPaidAppsFeed]];
self.appListFeedConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];

NSAssert(self.appListFeedConnection != nil, @"Failure to create URL connection.");

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;


return YES;
}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
self.appListData = [NSMutableData data]; 
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
 [self.appListData appendData:data];  // append incoming data
}


 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    if ([error code] == kCFURLErrorNotConnectedToInternet)
    {

        NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"No Connection Error"
                                                         forKey:NSLocalizedDescriptionKey];
    NSError *noConnectionError = [NSError errorWithDomain:NSCocoaErrorDomain
                                                     code:kCFURLErrorNotConnectedToInternet
                                                 userInfo:userInfo];
    [self handleError:noConnectionError];
}
else
{
    // otherwise handle the error generically
    [self handleError:error];
}

self.appListFeedConnection = nil;   // release our connection
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
self.appListFeedConnection = nil;   // release our connection

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

// create the queue to run our ParseOperation
self.queue = [[NSOperationQueue alloc] init];

// create an ParseOperation (NSOperation subclass) to parse the RSS feed data
// so that the UI is not blocked
parseOperation *parser = [[parseOperation alloc] initWithData:self.appListData];

parser.errorHandler = ^(NSError *parseError) {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self handleError:parseError];
    });
};

// Referencing parser from within its completionBlock would create a retain
// cycle.
__weak parseOperation *weakParser = parser;

parser.completionBlock = ^(void) {
    if (weakParser.appRecordList) {
        // The completion block may execute on any thread.  Because operations
        // involving the UI are about to be performed, make sure they execute
        // on the main thread.
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"completionBlock");

            NSLog(@"weakParser.appRecordList====%@", weakParser.appRecordList);//null

            self.entries=[[NSMutableArray alloc]init];
            self.entries = weakParser.appRecordList;
                            NSLog(@"self.entries====%@", self.entries);//null
            NSLog(@"weakParser.appRecordList====%@", weakParser.appRecordList);

        });
    }

    // we are finished with the queue and our ParseOperation
    self.queue = nil;
};

[self.queue addOperation:parser]; // this will start the "ParseOperation"

// ownership of appListData has been transferred to the parse operation
// and should no longer be referenced in this thread
self.appListData = nil;
}

@end
@接口AppDelegate:UIResponder
{
UINavigationController*nav;
}
@属性(强,非原子)UIWindow*window;
@属性(非原子,强)NSMutableArray*项;
@结束
#导入“AppDelegate.h”
静态NSString*const TopPaidAppsFeed=@“http:url”;
@接口AppDelegate()
@属性(非原子,强)NSOperationQueue*队列;
@属性(非原子,强)NSOperationQueue*queue1;
@属性(非原子,强)NSURLConnection*appListFeedConnection;
@属性(非原子,强)NSMutableData*appListData;
@结束
@实现AppDelegate
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项
{
NSURLRequest*urlRequest=[nsurlRequestRequestWithURL:[nsurlUrlWithString:TopPaidAppsFeed]];
self.appListFeedConnection=[[NSURLConnection alloc]initWithRequest:urlRequest委托:self];
NSAssert(self.appListFeedConnection!=nil,@“无法创建URL连接”);
[UIApplication sharedApplication].networkActivityIndicatorVisible=是;
返回YES;
}
-(void)连接:(NSURLConnection*)连接DidReceiverResponse:(NSURResponse*)响应
{
self.appListData=[NSMutableData];
}
-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据
{
[self.appListData appendData:data];//追加传入数据
}
-(无效)连接:(NSURLConnection*)连接失败错误:(NSError*)错误
{
[UIApplication sharedApplication].networkActivityIndicatorVisible=否;
如果([错误代码]==KCFurrerRorNotConnectedPointerNet)
{
NSDictionary*userInfo=[NSDictionary Dictionary WithObject:@“无连接错误”
forKey:NSLocalizedDescriptionKey];
NSError*noConnectionError=[NSErrorErrorWithDomain:NSCOCAerorDomain
代码:KCFurrerRorNotConnectedPointerNet
userInfo:userInfo];
[自处理错误:无连接错误];
}
其他的
{
//否则,一般地处理错误
[自处理错误:错误];
}
self.appListFeedConnection=nil;//释放我们的连接
}
-(无效)连接IDFinishLoading:(NSURLConnection*)连接
{
self.appListFeedConnection=nil;//释放我们的连接
[UIApplication sharedApplication].networkActivityIndicatorVisible=否;
//创建队列以运行解析操作
self.queue=[[NSOperationQueue alloc]init];
//创建一个ParseOperation(NSOperation子类)来解析RSS提要数据
//这样UI就不会被阻塞
parseOperation*parser=[[parseOperation alloc]initWithData:self.appListData];
parser.errorHandler=^(NSError*parseError){
dispatch\u async(dispatch\u get\u main\u queue()^{
[自处理错误:解析错误];
});
};
//从其completionBlock中引用解析器将创建一个retain
//循环。
__弱解析操作*weakParser=parser;
parser.completionBlock=^(无效){
if(弱帕瑟程序appRecordList){
//完成块可以在任何线程上执行。因为
//涉及即将执行的UI,请确保它们已执行
//在主线程上。
dispatch\u async(dispatch\u get\u main\u queue()^{
NSLog(@“completionBlock”);
NSLog(@“weakParser.appRecordList===%@”,weakParser.appRecordList);//null
赛尔夫恩
if (weakParser.appRecordList) {
        self.entries=[[NSMutableArray alloc]init];
        self.entries = weakParser.appRecordList;
}