应用程序冻结,ui线程冲突被怀疑

时间:2013-11-15 01:21:15

标签: objective-c multithreading user-interface admob advertising

我的应用有时会冻结。只有Admob广告会像往常一样继续移动。因此,我们怀疑问题与广告和线程冲突有关。这是我实现横幅的代码:

    - (void)viewDidLoad{
    [super viewDidLoad];

    version = [[versionObj alloc]init];
    version = [[Resultados sharedResultados]version];

    alreadySawVideo = NO;

    manager = [[SecondViewManager alloc]init];


    dispatch_async(dispatch_get_main_queue(), ^{
        //[self showads];
        if(version.pro == 0) {

            //sets the size of tabla
            tabla.frame = CGRectMake(tabla.frame.origin.x, tabla.frame.origin.y, tabla.frame.size.width, tabla.frame.size.height-adHeight);

            admob *admobObj = [[admob alloc]init];
            admobBan = [admobObj showAdvertisement:[NSString stringWithFormat:@"secondViewBanner" ]];
            admobBan.rootViewController = self;
            [self.view addSubview:admobBan];
        }

    });


}

在另一个班级的其他地方,方法:

-(GADBannerView *)showAdvertisement: (NSString *)type{

    version = [[versionObj alloc]init];
    version = [[versionObjSingleton sharedversionObjSingleton] version];

        //organizes the link for planetary resources.
        CGRect screenRect = [[UIScreen mainScreen] bounds];

        //screen dimensions
        CGFloat viewHeight = screenRect.size.height;
        CGFloat viewWidth = screenRect.size.width;

        textViewHyperLink.frame = CGRectMake(0.0, viewHeight - GAD_SIZE_320x50.height-144, 160, 31);
        textViewHyperLink2.frame = CGRectMake(viewWidth/2, viewHeight - GAD_SIZE_320x50.height-144, GAD_SIZE_320x50.width-160, 31);;


        //organizes the link for the advertisement.
        screenRect = [[UIScreen mainScreen] bounds];

        if(![type caseInsensitiveCompare:@"pdfMap"]){
            admobBan = [[GADBannerView alloc] initWithFrame:CGRectMake(0,viewHeight-GAD_SIZE_320x50.height - tabBarHeight, GAD_SIZE_320x50.width, GAD_SIZE_320x50.height)];
        }else if(![type caseInsensitiveCompare:@"specificRutasList"]){
            admobBan = [[GADBannerView alloc] initWithFrame:CGRectMake(0,0, GAD_SIZE_320x50.width, GAD_SIZE_320x50.height)];
        }else{//std view.
            admobBan = [[GADBannerView alloc] initWithFrame:CGRectMake(0, viewHeight - GAD_SIZE_320x50.height - 113+65, GAD_SIZE_320x50.width, GAD_SIZE_320x50.height)];
        }

        if(![type caseInsensitiveCompare:@"mainMenu"]){
            admobBan.adUnitID = AdMod_ID_main;
        }else if(![type caseInsensitiveCompare:@"interactiveMap"]){
            admobBan.adUnitID = AdMob_ID_interactiveMap;
        }else if(![type caseInsensitiveCompare:@"mainRutas"]){
            admobBan.adUnitID = AdMob_ID_mainRutas;
        }else if(![type caseInsensitiveCompare:@"rutasMap"]){
            admobBan.adUnitID = AdMob_ID_rutasMap;        
        }else if(![type caseInsensitiveCompare:@"pdfMap"]){
            admobBan.adUnitID = AdMob_ID_pdfMap;
        }else if(![type caseInsensitiveCompare:@"realtimeMapBanner"]){
            admobBan.adUnitID = AdMob_ID_realtimeMap;
        }else if(![type caseInsensitiveCompare:@"secondViewBanner"]){
            admobBan.adUnitID = AdMob_ID_secondViewBanner;
        }else if(![type caseInsensitiveCompare:@"specificRutasList"]){
            admobBan.adUnitID = AdMob_ID_specificRutasList;
        }

        admobBan.rootViewController = self;

        GADRequest *request = [GADRequest request];

        request.gender = kGADGenderFemale;

        [request setBirthdayWithMonth:3 day:13 year:1991];

        //posicion de bogota:
        if (version.ciudad == 0) {
            [request setLocationWithLatitude:version.latitude
                                   longitude:version.longitude
                                    accuracy:1];
        }else if(version.ciudad == 1){// its medellin
            [request setLocationWithLatitude:version.latitude
                                   longitude:version.longitude
                                    accuracy:1];
        }else if(version.ciudad == 2){
            [request setLocationWithLatitude:version.latitude
                                   longitude:version.longitude
                                    accuracy:1];
        }

        [admobBan loadRequest:[GADRequest request]];

    return admobBan;
}

1 个答案:

答案 0 :(得分:1)

您无法使用主队列,否则您将冻结该应用。 使用此队列:

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^(void) {
    if(version.pro == 0) {

        //sets the size of tabla
        tabla.frame = CGRectMake(tabla.frame.origin.x, tabla.frame.origin.y, tabla.frame.size.width, tabla.frame.size.height-adHeight);

        admob *admobObj = [[admob alloc]init];
        admobBan = [admobObj showAdvertisement:[NSString stringWithFormat:@"secondViewBanner" ]];
        admobBan.rootViewController = self;
        [self.view addSubview:admobBan];
    }
});

主队列仅用于更新视图,您也可以使用:

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^(void) {
    if(version.pro == 0) {

        //sets the size of tabla
        tabla.frame = CGRectMake(tabla.frame.origin.x, tabla.frame.origin.y, tabla.frame.size.width, tabla.frame.size.height-adHeight);

        admob *admobObj = [[admob alloc]init];
        admobBan = [admobObj showAdvertisement:[NSString stringWithFormat:@"secondViewBanner" ]];


        dispatch_async(dispatch_get_main_queue(), ^{
            admobBan.rootViewController = self;
            [self.view addSubview:admobBan];
        });

    }
});