0

In a view I am showing images which I am fetching from coredata, The images I am showing using carousel classes. In the same view I have facebook button which will navigate me to another screen. When I am clicking the Facebook button then the action is not getting performed, after the images are loaded only the next screen is coming, the button action is not getting called as soon as I click on it.

In viewDidLoad I have called:

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

The popularImages method:

- (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];
}

The button I have added in xib, How can I overcome this?

4

2 回答 2

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];
}

希望它有帮助:)

于 2013-08-27T04:08:45.080 回答
0

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

于 2013-08-27T05:35:24.830 回答