I want to append json object I save in core data into an array, but it's not working with append. how I can append core data object into an array.
this is my array
private var videos = [Video]()
this my function to fetch an api and store the json into core data
let params = ["part": "snippet", "q": "tausiyah \(name)", "key": "AIzaSyC2mn0PTL8JmSWEthvksdJLvsnwo5Tu9BA"]
APIServices.shared.fetchData(url: APIServices.youtubeBaseURL, params: params, of: Item.self) { (items) in
items.forEach({ (item) in
print(item.id.videoId)
let privateContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
privateContext.parent = CoreDataManager.shared.persistenceContainer.viewContext
let video = Video(context: privateContext)
video.title = item.snippet.title
video.videoId = item.id.videoId
do {
try privateContext.save()
try privateContext.parent?.save()
self.videos.append(video) // this is I can't append core data into my array
} catch let saveErr {
print("Failed to save json data:", saveErr)
}
})
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
DispatchQueue.main.asyncinside the fetch data closure.numberOfItems(in section:)code