2012-07-09 94 views
0

我正在使用dispatch_async進行後臺上傳。從另一個Stackoverflow問題,我發現dispatch_asyncPerformSelectorInBackground更好。但是現在我調用它之後,UI變得非常緩慢。任何人都可以爲此提供解決方案嗎?我使用的代碼被添加如下:dispatch_async減慢界面和應用程序

__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

你確定你沒有發佈到UI隊列嗎?你應該創建自己的隊列。 – Dani 2012-07-09 04:55:45

+1

我不知道是什麼導致了你的問題,但是如果你只在塊中讀取someString,你不需要'__block'。如果要在塊內更改該變量的值,則使用__block。 – 2012-07-09 04:58:04

+0

@Dani:全局隊列與主隊列絕對不一樣。 – 2012-07-09 05:27:24

回答

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 
         }); 
        } 
       }); 
相關問題