【发布时间】:2017-05-08 10:01:16
【问题描述】:
我有这两个功能
//function for updating the group list groupIds
func updateFriendGroupList(friendId: String, groupIds: [String]) {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let friendGroups = FriendGroups(context: context)
for i in 0..<groupIds.count {
friendGroups.friendId = friendId
friendGroups.groupId = groupIds[i]
}
(UIApplication.shared.delegate as! AppDelegate).saveContext()
}
//function for fetching group list groupIds
func fetchFriendGroupList(friendId: String) -> ([String]) {
var groupIds = [String]()
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
self.fetchFriendGroupListEntity.removeAll()
do {
self.fetchFriendGroupListEntity = try context.fetch(FriendGroups.fetchRequest())
} catch {
print("Fetching Failed")
}
for i in 0..<self.fetchFriendGroupListEntity.count {
if self.fetchFriendGroupListEntity[i].friendId == friendId {
groupIds.append(self.fetchFriendGroupListEntity[i].groupId!)
}
}
//returns an array containing groupIds
return groupIds
}
我检查了 updateFriendGroupList 中保存的 groupId 的数量。例如 2。但在我的检索函数中,计数始终为 1。
尽管保存了多个 groupId,但每次获取它们时我只得到 1 个 groupId。我错过了什么?
【问题讨论】:
标签: swift xcode core-data swift3 xcode8