2013-12-08 70 views
0

我的應用程序被刷新的Twitter鳴叫當收到這個錯誤:*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object' *** First throw call stack:錯誤刷新微博鳴叫

下面是代碼:

- (void)fetchInstagramPics { 
    instaPics = [[NSMutableArray alloc] init]; 
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults]; 
    BOOL *status = [user boolForKey:@"isInstagramLoggedIn"]; 
    if (status) { 
     [[InstagramClient sharedClient] getPath:@"users/self/feed" 
            parameters:nil 
             success:^(AFHTTPRequestOperation *operation, id responseObject) { 
              // NSLog(@"Response: %@", responseObject); 
              self.timelineResponse = [responseObject mutableCopy]; 
              [self.instaPics addObjectsFromArray:responseObject[@"data"]]; 
              [self updateArrays]; 
              [self.tableView reloadData]; 
             } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
              NSLog(@"Failure: %@", error); 
             }]; 
    } 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [self.tableView reloadData]; }); 


} 


- (void)updateArrays { 
    instaPics = self.timelineResponse[@"data"]; 
    totalFeed = [tweets arrayByAddingObjectsFromArray:instaPics]; 

} 

- (void)fetchTimeline { 
    [self fetchInstagramPics]; 
    [self fetchTweetsAuth]; 
    [self updateArrays]; 
    [self.tableView reloadData]; 
} 

- (void)refetchTimeline { 
    [self fetchInstagramPics]; 
    [self fetchTweets]; 
    [self updateArrays]; 
    [self.tableView reloadData]; 

} 

- (void)fetchTweetsAuth { 
    self.twitterClient = [[AFOAuth1Client alloc] initWithBaseURL:[NSURL URLWithString:@"https://api.twitter.com/1.1/"] key:@"4oFCF0AjP4PQDUaCh5RQ" secret:@"NxAihESVsdUXSUxtHrml2VBHA0xKofYKmmGS01KaSs"]; 

    [self.twitterClient authorizeUsingOAuthWithRequestTokenPath:@"/oauth/request_token" userAuthorizationPath:@"/oauth/authorize" callbackURL:[NSURL URLWithString:@"floadt://success"] accessTokenPath:@"/oauth/access_token" accessMethod:@"POST" scope:nil success:^(AFOAuth1Token *accessToken, id responseObject) { 
     [self.twitterClient registerHTTPOperationClass:[AFJSONRequestOperation class]]; 
     [self.twitterClient getPath:@"statuses/home_timeline.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { 
      NSArray *responseArray = (NSArray *)responseObject; 
      [responseArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
       NSLog(@"Success: %@", obj); 
       tweets = responseArray; 
      }]; 
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
      NSLog(@"Error: %@", error); 
     }]; 

    } failure:^(NSError *error) { 
     NSLog(@"Error: %@", error); 
    }]; 
} 

- (void)fetchTweets{ 
     [self.twitterClient registerHTTPOperationClass:[AFJSONRequestOperation class]]; 
     [self.twitterClient getPath:@"statuses/home_timeline.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { 
      NSArray *responseArray = (NSArray *)responseObject; 
      [responseArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
       NSLog(@"Success: %@", obj); 
       tweets = [tweets copy]; 
       tweets = responseArray; 
      }]; 
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
      NSLog(@"Error: %@", error); 
     }]; 
} 

- (void)refresh:(UIRefreshControl *)refreshControl { 
    [self refetchTimeline]; 
    [refreshControl endRefreshing]; 
} 

tweets陣列與一個instaPics陣列是所謂的陣列下共享totalArray這就是爲什麼我包含了fetchInstagramPics函數。我知道這聽起來很複雜,但您的幫助深表謝意。

這裏是堆棧跟蹤:

STACK TRACE : 
(
    0 CoreFoundation      0x02b6b5e4 __exceptionPreprocess + 180 
    1 libobjc.A.dylib      0x027978b6 objc_exception_throw + 44 
    2 CoreFoundation      0x02b6b3bb +[NSException raise:format:] + 139 
    3 CoreFoundation      0x02bf2365 -[__NSCFArray insertObject:atIndex:] + 101 
    4 CoreFoundation      0x02b2f2d0 -[NSMutableArray insertObjects:count:atIndex:] + 208 
    5 CoreFoundation      0x02b2ef69 -[NSMutableArray insertObjectsFromArray:range:atIndex:] + 425 
    6 CoreFoundation      0x02b2ed15 -[NSMutableArray addObjectsFromArray:] + 661 
    7 MyApp        0x0003c6c6 __42-[StreamViewController fetchInstagramPics]_block_invoke + 278 
    8 MyApp        0x0001e73b __64-[AFJSONRequestOperation setCompletionBlockWithSuccess:failure:]_block_invoke91 + 43 
    9 libdispatch.dylib     0x035187f8 _dispatch_call_block_and_release + 15 
    10 libdispatch.dylib     0x0352d4b0 _dispatch_client_callout + 14 
    11 libdispatch.dylib     0x0351b75e _dispatch_main_queue_callback_4CF + 340 
    12 CoreFoundation      0x02bd0a5e __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14 
    13 CoreFoundation      0x02b116bb __CFRunLoopRun + 1963 
    14 CoreFoundation      0x02b10ac3 CFRunLoopRunSpecific + 467 
    15 CoreFoundation      0x02b108db CFRunLoopRunInMode + 123 
    16 GraphicsServices     0x045309e2 GSEventRunModal + 192 
    17 GraphicsServices     0x04530809 GSEventRun + 104 
    18 UIKit        0x0192ad3b UIApplicationMain + 1225 
    19 MyApp        0x00062b1d main + 141 
    20 libdyld.dylib      0x037bf70d start + 1 
) 
+0

我已經更新了答案 –

回答

0

在你-[AppDelegate didFinishLaunchingWithOptions:]

#ifdef DEBUG 
    NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler); 
#endif 

在文件的底部AppDelegate中,下面的所有方法:

#ifdef DEBUG 
void uncaughtExceptionHandler(NSException *ex) 
{ 
    NSLog(@"CRASH :\n %@", ex.reason); 
    NSLog(@"STACK TRACE :\n %@", ex.callStackSymbols); 
    exit(0); 
} 
#endif 

這樣你將在控制檯中看到堆棧跟蹤。之後,你會發現什麼功能導致崩潰並修復它,或者通過發佈stacktrace來編輯你的問題,讓我們看看它。
編輯看完你的堆棧跟蹤後:
你做[self.instaPics addObjectsFromArray:responseObject[@"data"]];,但是你讓instaPics = [[NSMutableArray alloc] init];。我相信你的self.instaPics是不可改變的NSArray。我不知道,你的邏輯應該如何工作,你必須要麼self.instaPics = [[NSMutableArray alloc] init];[instaPics addObjectsFromArray:responseObject[@"data"]];

+0

謝謝你的建議我已經添加了默認問題的堆棧跟蹤 – Prad