performselectorinBackground 不工作

Posted

技术标签:

【中文标题】performselectorinBackground 不工作【英文标题】:performselectorinBackground is not working 【发布时间】:2013-08-27 02:23:36 【问题描述】:

在一个视图中,我正在显示从 coredata 获取的图像,我正在使用轮播类显示的图像。在同一个视图中,我有 facebook 按钮,可以将我导航到另一个屏幕。当我单击 Facebook 按钮时,没有执行该操作,加载图像后只有下一个屏幕出现,单击按钮后不会立即调用该按钮操作。

在 viewDidLoad 中我调用了:

[self performSelectorInBackground:@selector(popularImages) withObject:nil];

popularImages 方法:

- (void)popularImages

    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Deals"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ispopular == 1"];
    [request setPredicate:predicate];
    NSLog(@"popular Hypes called");
    [self.managedObjectContext executeFetchRequest:request returnManagedObjectIDs:NO onSuccess:^(NSArray *deals) 
        deals123 = [[NSMutableArray alloc] initWithArray:deals];
        ij = 1;

        for (Deals *deals in deals123) 
            customizedMessage = [deals valueForKeyPath:@"txtmsgrec"];
            dealIdString = [deals valueForKeyPath:@"dealsId"];
            descriptionStr = [deals valueForKeyPath:@"dealdescription"];
            captionStrArray = [deals valueForKeyPath:@"dealsassets.caption"];

            [customizedMessageDict setObject:customizedMessage forKey:[NSString stringWithFormat:@"dealid%d",ij]];
            [dealDict setObject:customizedMessageDict forKey:@"dealsmessage"];

            [dealsIdDict setObject:dealIdString forKey:[NSString stringWithFormat:@"dealid%d",ij]];
            [dealDict setObject:dealsIdDict forKey:@"dealsId"];

            NSDictionary *dict = [NSDictionary dictionaryWithObject:captionStrArray forKey:@"caption"];
            NSArray *captionTemparr = [[dict objectForKey:@"caption"] allObjects];

            NSArray *assets = [deals valueForKeyPath:@"dealsassets.dealassetid"];
            NSDictionary *dealassetidDict = [NSDictionary dictionaryWithObject:assets forKey:@"dealassetid"];
            NSArray *dealassetidTemparr = [[dealassetidDict objectForKey:@"dealassetid"] allObjects];

            [dealsUrlDict setObject:[[dealassetidDict objectForKey:@"dealassetid"] allObjects] forKey:[NSString stringWithFormat:@"dealid%d",ij]];
            [dealDict setObject:dealsUrlDict forKey:@"delasURL"];
            [dealsdescriptionDict setObject:descriptionStr forKey:[NSString stringWithFormat:@"dealid%d",ij]];
            [dealDict setObject:dealsdescriptionDict forKey:@"dealsDescription"];
            [dealscaptionDict setObject:captionTemparr forKey:[NSString stringWithFormat:@"dealid%d",ij]];
            [dealDict setObject:dealscaptionDict forKey:@"dealscaption"];

            ij++;
                
        NSArray *array = [[dealDict valueForKey:@"delasURL"]allKeys];
        NSLog(@"allKeys]class %@",[[[dealDict valueForKey:@"delasURL"]allKeys]class]);
        NSDictionary *dealurlDict = [dealDict valueForKey:@"delasURL"];
        NSDictionary *dealDescDict = [dealDict valueForKey:@"dealsDescription"];
        NSDictionary *dealCaptionDict = [dealDict valueForKey:@"dealscaption"];
        NSDictionary *dealIDDict = [dealDict valueForKey:@"dealsId"];
        NSDictionary *dealMessageDict = [dealDict valueForKey:@"dealsmessage"];

        for(int i=0;i<[array count];i++)
        
            NSString *keyvalue = (NSString *)[array objectAtIndex:i];
            NSString *dealId = [dealIDDict valueForKey:keyvalue];
            NSArray *dealUrl = [dealurlDict valueForKey:keyvalue];
            NSString *dealdesc = [dealDescDict valueForKey:keyvalue];
            NSArray *dealcaption = [dealCaptionDict valueForKey:keyvalue];
            NSString *dealMessage = [dealMessageDict valueForKey:keyvalue];

            if([dealUrl count] == [dealcaption count])
            
                //[dealUrl enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop)
                for(int j = 0; j<[dealUrl count]; j++)
                
                    PopularHypesModal *dobject = [[PopularHypesModal alloc]init];
                    dobject.dealsID = dealId;
                    dobject.dealDescription = dealdesc;
                    dobject.dealcaption = [dealcaption objectAtIndex:j];
                    dobject.dealsUrl = [dealUrl objectAtIndex:j];
                    dobject.dealsMessage = dealMessage;
                    [self.items addObject:dobject];
                    [dobject release];
                
            
            else
            
                //[dealUrl enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop)
                for(int j=0;j<[dealUrl count];j++)
                
                    PopularHypesModal *dobject=[[PopularHypesModal alloc]init];
                    dobject.dealsID = dealId;
                    dobject.dealDescription = dealdesc;
                    dobject.dealsMessage = dealMessage;
                    if([dealcaption count]>0)
                    
                        dobject.dealcaption =[dealcaption objectAtIndex:0];
                    
                    dobject.dealsUrl=[dealUrl objectAtIndex:j];
                    [self.items addObject:dobject];
                    [dobject release];
                
                // ];
            
        
        //];

        carousel.dataSource = self;
        carousel.delegate = self;

    onFailure:^(NSError *error) 
        NSLog(@"Error fetching: %@", error);
        popularHypes = YES;
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Server Problem" message:@"Sorry, There is some Error with server,Please try after some time" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
        [alert show];
        [alert release];
        ij++;
    ];//1st

    [dealDict retain];

我在xib中添加的按钮,我该如何克服?

【问题讨论】:

【参考方案1】:

在后台线程上运行应该不会给您带来任何问题,除非您尝试从同一个后台线程更新 UI 元素。确保将任何与 UI 相关的活动转发到主线程。例如:

- (void)viewWillAppear:(BOOL)animated 
    [self performSelectorInBackground:@selector(refreshTableView)];
    [super viewWillAppear:animated];


- (void)refreshTableView 
    // YourCode
    [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];

希望对你有帮助:)

【讨论】:

【参考方案2】:

请试试这个 NSThread 方法 [NSThread detachNewThreadSelector:@selector(RunMethodEvenWhenMinimized) toTarget:self withObject:dict];

【讨论】:

不,它不工作,同样的事情正在发生,它需要一些时间才能移动到 facebook 屏幕。

以上是关于performselectorinBackground 不工作的主要内容,如果未能解决你的问题,请参考以下文章