This function is taking a tweet handed to it by the Swifter library in the form of a chunk of JSON, creating a new Tweet object from that JSON, then adding the tweet, in the proper order, to the array 'tweetsList'. Each tab (a tab is a data structure encapsulating a collection of tweets) calls this function on itself when it is told to update, if new tweets are found. All tabs are told to update in viewDidLoad, so this should execute five times, as there are five tabs by default. The app chokes on the first call to this, so it's not random or only happening on certain tweets. The full function:
func addTweetWithJSONValue(JSONTweet:JSONValue) {
let newTweet=Tweet(JSONObject:JSONTweet, isDM:type==TweetsTabTypes.messages)
//if no tweets are yet stored, just add this one
if tweetsList.count==0 {
tweetsList.append(newTweet)
checkForMaxIDUpdateWithNewID(newTweet.idAsString)
checkForSinceIDUpdateWithNewID(newTweet.idAsString)
return
}
let mostRecentTweet=tweetsList[tweetsList.count-1]
if let mostRecentTweetID=mostRecentTweet.JSONObject["id_str"].string, newTweetID=newTweet.JSONObject["id_str"].string {
if newTweetID == mostRecentTweetID { //we already have this tweet, so don't add it again
print("Repeated tweet detected! Ignoring it and not adding it to the list.")
return
}
//now add the tweet. If the ID is newer, put it on top, else put it on the bottom.
(Int(newTweetID)! < Int(mostRecentTweetID)! ? tweetsList.append(newTweet) : tweetsList.insert(newTweet, atIndex:0) ) //offending line
checkForMaxIDUpdateWithNewID(newTweet.idAsString)
checkForSinceIDUpdateWithNewID(newTweet.idAsString)
} else { //ID for this tweet was nil or couldn't be retrieved
tweetsList.append(newTweet)
print("No ID found while inserting a tweet. It was appended instead.")
}
}