0
私はCS193スタンフォードコースを修了し、コアデータを使用してTwitterクライアントの一部としてツイートを保存しています。コアデータを使用してエンティティのカウントを増やす(Twitter CS193p)
しかし、私が存在するhashmentionを見つけたら、どれくらいの一致を表すhash.countをインクリメントしたいのですが、hash.countがいくつあっても0または2しか格納しません。その属性はエンティティ上の永続ストレージとして機能していません)。
class HashMention: NSManagedObject {
static func findOrCreateHashMention(matching twitterInfo: Twitter.Mention, in context: NSManagedObjectContext) throws -> HashMention
{
let hash = HashMention (context: context)
let request : NSFetchRequest<HashMention> = HashMention.fetchRequest()
request.predicate = NSPredicate(format: "text =[cd] %@", twitterInfo.keyword)
do {
let matches = try context.fetch(request)
if matches.count > 0 {
//inc count
hash.count = Int32(Int(matches.count) + 1)
return hash
}
else{
hash.count = 0
print("zero hash:", twitterInfo.keyword)
hash.text = twitterInfo.keyword.lowercased()
return hash
}
}
catch{
//makes this function throw
throw error
}
}
}
ある時点でコンテキストを保存していますか? – ghostatron
いいえ、それはいつ行うのがいいですか? – stevenpcurtis
全体のシナリオに少し依存しますが、そこにあるif/elseブロックの直後に(「do」節を残す直前に)1つのオプションがあります。 – ghostatron