title: iOS - Twitter Connectivity Issue: "The operation couldn’t be completed. (Cocoa error 3840.)"
body: <p>I am creating an app displaying my twitter timeline. But I sometimes get the following error:</p>
  
  <pre><code>Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x1fd807c0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
  </code></pre>
  
  <p>I am using the following code to retrieve the timeline of twitter:</p>
  
  <pre><code>-(void) getTimeLine{
  
  ACAccountStore *account=[[ACAccountStore alloc] init];
  ACAccountType *accountType=[account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
  [activityIndicatorLoadTweets setHidden:NO];
  [activityIndicatorLoadTweets startAnimating];
  [account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error)
  {
      if (granted==YES) {
          NSArray *arrOfAccounts=[account accountsWithAccountType:accountType];
          if ([arrOfAccounts count]!=0)
          {
              ACAccount *twitterAccount=[arrOfAccounts lastObject];
              NSURL *requestURL=[NSURL URLWithString:@"http://api.twitter.com/1/statuses/home_timeline.json"];
              NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
              [params setObject:@"20" forKey:@"count"];
              [params setObject:@"1" forKey:@"include_entities"];
  
              SLRequest *postRequest=[SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:requestURL parameters:params];
  
              postRequest.account=twitterAccount;
  
              [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
              {
                  if (responseData==nil) {
                      NSLog(@"Could not connect. Try Again.");
                      [self showLabelAndStartTimer];
                      lblStatus.text=@"Could not connect at the moment. Please try Again.";
                      [activityIndicatorLoadTweets setHidden:YES];
                      [activityIndicatorLoadTweets stopAnimating];
                  }
                  else{
                      lblStatus.hidden=YES;
                      self.arrDataSource=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&amp;error];
  
                      if ([self.arrDataSource count]!=0)
                      {
                          dispatch_async(dispatch_get_main_queue(), ^
                          {
                              [self.tableTweetsView reloadData];
                              [activityIndicatorLoadTweets setHidden:YES];
                              [activityIndicatorLoadTweets stopAnimating];
                              //[self fetchBollyTweetsFrom:self.arrDataSource];
                          });
                      }
                  }
  
              }];
  
          }
      }
      else{
          NSLog(@"Failure to access acount. Please check your internet connection.");
          [activityIndicatorLoadTweets setHidden:YES];
          [activityIndicatorLoadTweets stopAnimating];
      }
  
  
  }];
  
  }
  </code></pre>
  
  <p>Please note that the error description is from the line:</p>
  
  <pre><code>self.arrDataSource=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&amp;error];
  </code></pre>
  
  <p>The error is mostly self explanatory but If my JSON is wrong, why I am getting successful response half of the time?
  Comments and suggestions on the code are also welcome as I am newbie in iOS programming and using JSON.
  Thanks.</p>