Jonathan Tuzman
Jonathan Tuzman

Reputation: 13278

CS193P Smashtag Popularity Extra Task #3 - Get only new tweets using "IN" keyword

I'm working on Stanford CS193p's SmashTag Popularity Mentions assignment (asst. #5) and I've got everything working well. I'm working on Extra Task #3:

Loading up a lot of data by querying for an existing instance in the database, then inserting if not found over and over again, one by one (like we did in lecture), can be pretty poor performing. Enhance your application to make this more efficient by checking for the existence of a pile of things you want to be in the database in one query (then only creating the ones that don’t exist). The predicate operator IN might be of value here.

I've managed to do something like this, but not using the IN predicate operator! This is inside the perform block of my updateDatabase(with newTweets:) method: (I've called my core data entity CDTweet

if let databaseTweets = try? globalContext!.fetch(NSFetchRequest<CDTweet>(entityName: "CDTweet")) {
            let databaseIDs = databaseTweets.map { $0.id! }
            for twitterInfo in newTweets where !databaseIDs.contains(twitterInfo.id) {
                _ = CDTweet.createCDTweet(with: twitterInfo, into: globalContext!)
            }
        }

As you can see, I get all the tweets in the database, get their IDs, and then only create a new tweet for internet-fetched-Twitter.Tweets whose IDs are not in the array of database tweets.

This appears to function properly (i.e., create only the new tweets), but I am very curious how the instructor imagined it would work with the IN predicate operator. Has anyone done this and can lend some insight?

Note: A number of solutions I've seen have a core data entity for the Search Term (usually called Query). I don't have this, only entities for Tweet and Mention, and I have everything working fine.

Upvotes: 0

Views: 108

Answers (1)

Andreas Oetjen
Andreas Oetjen

Reputation: 10209

You need something like this (i assume searchIDs is an array of values you are looking for):

var searchIDs = // ... an array of IDs you are searching for
var fetchRequest = NSFetchRequest<CDTweet>(entityName: "CDTweet")
let predicate = NSPredicate(format: "id IN %@", searchIDs)
fetchRequest.predicate = predicate
databaseTweets = try? globalContext!.fetch(fetchRequest) {
     // here you should only get all entries with IDs in the newTweets array
} 

Details about predicates can be found here, about predicate syntax especially here

Upvotes: 0

Related Questions