2012-07-09 2 views
0

백그라운드 업로드에 dispatch_async을 사용하고 있습니다. 다른 Stackoverflow 질문에서 나는 dispatch_asyncPerformSelectorInBackground보다 낫다고 알았습니다. 하지만 이제는 UI가 매우 느리게 진행됩니다. 누구나이 솔루션을 제공 할 수 있습니까?dispatch_async가 UI 및 앱 속도를 늦추십시오.

__block NSString *someString; 
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 
             (unsigned long)NULL), ^(void) { 

    if(someString != nil) 
    { 
     Class *className = [class sharedObject]; 
     [className sendContacts]; 

    } 

}); 

기능 코드 : 내가 사용했던 코드는 아래에 추가됩니다

-(void) sendContacts { 

    NSURL *url = [NSURL URLWithString:@"myurl"]; 

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 
    [request setRequestMethod:@"POST"]; 

    [request addRequestHeader:@"d" value:[[UIDevice currentDevice] uniqueIdentifier]]; 

    NSString *sessionKey = [[NSUserDefaults standardUserDefaults]valueForKey:@"UD_Sessionkey"]; 

    [request addRequestHeader:@"s" value:sessionKey]; 

    NSData *data=[[self createContactList] dataUsingEncoding: [NSString defaultCStringEncoding] ]; 

    NSMutableData *x = [[NSMutableData alloc] init]; 
    [x setData:data]; 

    [request setPostBody:x]; 

    [request setShouldContinueWhenAppEntersBackground:YES]; 
    [request setDelegate:self]; 
    [request setTimeOutSeconds:60]; 
    [request setDidFinishSelector:@selector(requestFinished:)]; 
    [request setDidFailSelector:@selector(requestFailed:)]; 
    [request startAsynchronous]; 

} 


- (void)requestFinished:(ASIHTTPRequest *)requestData 
{ 

    NSLog(@"Response : %@", [requestData responseString]); 

    if (self.currentLimit_ > totalCount) { 

     //do nothing 
    } else { 

     [self sendContacts]; 
    } 


} 

- (NSString *)createContactList 
{ 

    self.currentLimit_ = self.currentLimit_ + 10; 
    self.currentCount_ = self.currentCount_ + 1; 

    ABAddressBookRef addressBook = ABAddressBookCreate(); 
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook); 
    CFIndex nPeople = ABAddressBookGetPersonCount(addressBook); 

    totalCount = nPeople; 

    NSMutableString *requestContactsString = [[NSMutableString alloc] init]; 


    [requestContactsString appendString:@"<list>"]; 

    for (int i=currentCount; i<self.currentLimit_; i++) 
    { 
     NSLog(@"Started : %d", i); 
     if (self.currentLimit_ > totalCount) { 
      break; 
     } 

     if (i < self.currentLimit_ - 10) { 

      continue; 
     } 

     ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i); 
     CFTypeRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty); 
     CFTypeRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty); 
     CFTypeRef email = ABRecordCopyValue(ref, kABPersonEmailProperty); 
     CFTypeRef phone = ABRecordCopyValue(ref, kABPersonPhoneProperty); 

     //requestContactsString = [requestContactsString stringByAppendingFormat:@"<item>"]; 
     [requestContactsString appendString:@"<item>"]; 

     if(firstName) 
     { 
      [requestContactsString appendString:[NSString stringWithFormat:@"<firstname>%@</firstname>", firstName]]; 

      CFRelease(firstName); 
      firstName = nil; 
     } 
     if(lastName) 


      [requestContactsString appendString:[NSString stringWithFormat:@"<lastname>%@</lastname>", lastName]]; 
      CFRelease(lastName); 
      lastName = nil; 
     } 
     if(email) 
     { 
      if(ABMultiValueGetCount(email)>0) 
      { 
       CFTypeRef em = ABMultiValueCopyValueAtIndex(email, 0); 

       [requestContactsString appendString:[NSString stringWithFormat:@"<email>%@</email>", em]]; 

       CFRelease(em); 
      } 
      CFRelease(email); 
      email = nil; 
     } 
     if(phone) 
     { 
      if(ABMultiValueGetCount(phone)>0) 
      { 
       CFTypeRef ph = ABMultiValueCopyValueAtIndex(phone, 0); 

       [requestContactsString appendString:[NSString stringWithFormat:@"<phone>%@</phone>", ph]]; 
       CFRelease(ph); 
      } 
      CFRelease(phone); 
      phone = nil; 
     } 

     [requestContactsString appendString:@"</item>"]; 

    } 



    if(allPeople) 
    { 
     CFRelease(allPeople); 
     allPeople = nil; 
    } 
    if(addressBook) 
    { 
     CFRelease(addressBook); 
     addressBook = nil; 
    } 

    [requestContactsString appendString:@"</list>"]; 

    NSString *hashedContactsString = [self generateHashedPassword:requestContactsString]; 

    NSLog(@"Contacts : %@", requestContactsString); 

    //contact list has changed 
    if(![[[NSUserDefaults standardUserDefaults] valueForKey:@"contacts"] isEqualToString:hashedContactsString]) 
    { 
     return requestContactsString; 
    } 
    else return nil; 
} 
+0

정말 대기열에 게시하지 않습니까? 자신 만의 대기열을 만들어야합니다. – Dani

+1

문제의 원인은 모르겠지만 블록에서 someString 만 읽는다면'__block'은 필요 없습니다. '__block'은 블록 내에서 변수의 값을 변경하고자 할 때 사용됩니다. –

+0

@Dani : 글로벌 대기열은 확실히 메인 대기열과 다릅니다. –

답변

2

둔화 객체를 오토 릴리즈 할 수 있습니다 ... 더 많은 시간이 걸리는 기능 악기 도구에서 확인해야합니다.

//Can you try using autoreleasepool for the threaded function. 
    dispatch_async(yourQueue, ^{ 
        @autoreleasepool { 
         //Your code to be run in background 
         dispatch_async(dispatch_get_main_queue(), ^{ 
          //Any UI updates 
         }); 
        } 
       }); 
관련 문제