dispatch_async(NSSearchPathForDirectoriesInDomains无法正常工作后的dispatch_get_main_queue()

时间:2013-11-05 17:06:02

标签: ios objective-c grand-central-dispatch

首先非常感谢你的帮助。

我是ios开发的新手,我想要更新进度条,但它无效。

这有效,进度条会更新:

dispatch_async(dispatch_get_main_queue(), ^(void){
      [self.progressBar setProgress:[self.progressBar progress]+1 animated:YES];
      NSLog(@"PROGRESS:%f",[self.progressBar progress]);
  });
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

这不起作用,并且该块不显示日志。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        dispatch_async(dispatch_get_main_queue(), ^(void){
            [self.progressBar setProgress:[self.progressBar progress]+1 animated:YES];
            NSLog(@"PROGRESS:%f",[self.progressBar progress]);
        });

除此之外,我从dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^(void)方法

调用此函数

我还没有找到任何解决方案。有人知道这种奇怪的行为吗?

-------------更新完全代码-------------------

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self performSelector:@selector(downloadInfo:) withObject:nil afterDelay:0.5];
}

-(void) downloadInfo:(id) sender {
    NTSessionVariables *sharedManager = [NTSessionVariables sharedManager];
    NSString *token = [sharedManager token];

    if (token != nil) {
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", URL_SERVER, URL]];
        NSDictionary *json = [NTJSONUtils getJSONWithImei:@"123456789" AndToken:token];
        NSDictionary *resultLogin = [NTHttpUtils sendHTTPPostWithURL:url AndJSON:json];

        if ([[[resultLogin objectForKey:@"operacion"] objectForKey:@"error"] isEqualToString:@""]) {
            NSString *bodyString = [NTJSONUtils decryptBody:[[resultLogin objectForKey:@"operacion"] objectForKey:@"body"] AndKey:token];
            //Save in memory
            [NTSharedPrefs saveStringToUserDefaults:bodyString withKey:PLANS];


            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
                [self reallyCreateDirectories:@{ @"bodyString" : bodyString, @"token" : token }];
            });

        }



    }

}

- (void)reallyCreateDirectories:(NSDictionary *)dict
{
    [self createDirectories:dict[@"bodyString"] AndToken:dict[@"token"]];
}

-(void) createDirectories: (NSString*) bodyString AndToken:(NSString*) token{
    NSData *jsonData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *e;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&e];

    NSArray *list = [json objectForKey:@"list"];
    NSFileManager *filemgr;

    filemgr = [NSFileManager defaultManager];
    float progress = 1.0f / [list count];
    for (int i=0; i< [list count]; i++) {
        NSDictionary *plan = [list[i] objectForKey:@"plan"];
        NSNumber *idPlan = [plan objectForKey:@"idplan"];
        NSString *company = [plan objectForKey:@"company"];

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        dispatch_async(dispatch_get_main_queue(), ^(void){
            [self.progressBar setProgress:[self.progressBar progress]+1 animated:YES];
            NSLog(@"PROGRESS:%f",[self.progressBar progress]);
        });
        NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
        NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/DOC/%@",idPlan]];
        NSURL *dir = [NSURL fileURLWithPath:path];
        if ([filemgr createDirectoryAtURL: dir withIntermediateDirectories:YES attributes: nil error:nil] == YES) {
            //Creamos los ficheros
            NSString *jsonPlanString = [NTJSONUtils jsonToStringWithDictionary:flightplan];
            if ([filemgr createFileAtPath:[path stringByAppendingString:@"/data.json"] contents:[jsonPlanString dataUsingEncoding: NSUTF8StringEncoding] attributes:nil] == YES) {
                NSLog(@"File Created");
            }else{
                NSLog(@"plan not created");
            }

            //Create Directories
            NSURL *urlDir = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", URL_SERVER, URL_DOCUMENT]];
            NSDictionary *jsonDir = [NTJSONUtils getCompleteStructure:@"123456789" AndToken:token AndName:company AndIdPlan:(NSString *)idPlan];
            NSDictionary *resultDir = [NTHttpUtils sendHTTPPostWithURL:urlDir AndJSON:jsonDir];

            if ([[[resultDir objectForKey:@"operacion"] objectForKey:@"error"] isEqualToString:@""]) {
                NSString *bodyString = [NTJSONUtils decryptBody:[[resultDir objectForKey:@"operacion"] objectForKey:@"body"] AndKey:token];
                [self createDirectoriesWithPlan:bodyString AndPath:path AndToken:token AndIdPlan:idPlan.stringValue AndName:company];

            }       
        }
    }

}

0 个答案:

没有答案