Pull to refresh returns empty array in Swift 3 iOS 10 - ios10

I have recently migrated my iOS app to support iOS 10 with Swift 2.3 and have encountered an issue which was previously working fine on iOS 9 / Swift.
Problem goes like this:
I have a UITableView which loads up sections and rows, at the first instance everything loads fine. But when I do a pull to refresh i am clearing out the array by totalSectionData.removeAllObjects(). now when the refresh is executed it is throwing an error [__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array. It is obvious I am calling the function to reload the data from server after clearing out the array.
On further investigating and putting breakpoints I found that it is not going inside numberOfSectionsInTableView and numberOfRowsInSection while I do refresh; On the first time it does goes inside it. tableView and delegate are connected properly.
If i comment totalSectionData.removeAllObjects() then it works fine but I get duplicate rows.
It was working fine on previous iOS 9 / Swift 2, I tested the code again on previous version to double confirm.
Is there anything that's new in iOS 10 / Swift 2.3 that I am missing / doing wrong way.
Refresh code:
func refresh(sender:AnyObject)
{
// Code to refresh table view
tableView.allowsSelection = false
totalSectionData.removeAllObjects()
get_data_from_url(json_data_url)
}
After the JSON data is received I am calling reloadData
func do_table_refresh()
{
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
self.refreshControl.endRefreshing()
self.hideActivityIndicator()
self.tableView.allowsSelection = true
return
})
}
// Fetch JSON
func get_data_from_url(url:String)
{
let url:NSURL = NSURL(string: url)!
let session = NSURLSession.sharedSession()
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "GET"
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
tableView.allowsSelection = false
let task = session.dataTaskWithRequest(request) {
(
data, response, error) in
guard let _:NSData = data, let _:NSURLResponse = response where error == nil else {
print("error")
return
}
self.showActivityIndicator()
dispatch_async(dispatch_get_main_queue(), {
let json = NSString(data: data!, encoding: NSASCIIStringEncoding)
self.extract_json(json!)
// self.activityIndicator.stopAnimating()
return
})
}
task.resume()
}
func extract_json(data:NSString)
{
var parseError: NSError?
let jsonData:NSData = data.dataUsingEncoding(NSASCIIStringEncoding)!
let json: AnyObject?
do {
json = try NSJSONSerialization.JSONObjectWithData(jsonData, options: [])
} catch let error as NSError {
parseError = error
json = nil
}
if (parseError == nil)
{
if let list:NSArray = json as? NSArray
{
print ("Inside if let")
let lblArray:NSArray = (list.valueForKey("level_number") as?NSArray)!
let data = NSOrderedSet(array: lblArray as [AnyObject])
for value in data {
let resultPredicate : NSPredicate = NSPredicate(format: "level_number == %d", value.integerValue)
let arrayData:NSArray = list.filteredArrayUsingPredicate(resultPredicate)
let lblArray:NSArray = (arrayData.valueForKey("part_number") as?NSArray)!
let dataValue = NSOrderedSet(array: lblArray as [AnyObject])
for index in dataValue {
let resultPredicate : NSPredicate = NSPredicate(format: "part_number == %d", index.integerValue)
totalSectionData.addObject(arrayData.filteredArrayUsingPredicate(resultPredicate))
}
}
for i in 0 ..< list.count
{
if let data_block = list[i] as? NSDictionary
{
TableData.append(datastruct(add: data_block))
}
}
do
{
try read()
}
catch
{
}
do_table_refresh()
}
}
}

Related

Return response as object in swift

I have a function that connects to an API to retrieve data. The API takes two parameters accessCode (provided by user in a text box) and then UDID (UDID of their device). I can parse the data from within the function, but only locally. I need to store the values that are returned but am unsure on how to return them properly. Essentially I need this to return the json object as a dictionary (I think...) so it can be parsed outside of the async task. I've read through the swift documentation and that's where I found out how to do the requests, but I can't find a way to store the returned values in memory for access outside of the function.
func getResponse(accessCode:String, UDID:String, _ completion: #escaping (NSDictionary) -> ()) {
let urlPath = "https://apihosthere.com/api/validate?accessCode=" + accessCode + "&UDID=" + UDID
guard let url = URL(string: urlPath) else { return }
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else { return }
do {
if let jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary {
let results = jsonResult as? NSDictionary
print(results)
completion(results!)
}
} catch {
//Catch Error here...
}
}
task.resume()
}
First of all don't use NSDictionary in Swift, use native [String:Any] and declare the type as optional to return nil if an error occurs.
And never use .mutableContainers in Swift, the option is useless.
func getResponse(accessCode:String, UDID:String, completion: #escaping ([String:Any]?) -> Void)) {
let urlPath = "https://apihosthere.com/api/validate?accessCode=" + accessCode + "&UDID=" + UDID
guard let url = URL(string: urlPath) else { return }
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if let error = error else {
print(error)
completion(nil)
return
}
do {
if let jsonResult = try JSONSerialization.jsonObject(with: data!) as? [String:Any] {
print(jsonResult)
completion(jsonResult)
} else {
completion(nil)
}
} catch {
print(error)
completion(nil)
}
}
task.resume()
}
Your mistake is that you don't consider the closure, you have to execute the entire code inside the completion handler
#IBAction func StartWizard(_ sender: UIButton) {
//Store entered access code
let accessCode = AccessCodeField.text!
//Call API to validate Access Code
getResponse(accessCode:accessCode, UDID:myDeviceUDID) { [weak self] result in
if let accessCodeFound = result?["Found"] as? Bool {
print("Value of Found during function:")
//If access code is valid, go to License view
print(accessCodeFound)
if accessCodeFound {
//Load License View
DispatchQueue.main.async {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let licenseController = storyboard.instantiateViewController(identifier: "LicenseViewPanel")
self?.show(licenseController, sender: self)
}
}
}
}
}
Your completion closure should handle the obtained data. You would call the function like this:
getResponse(accessCode: "code", UDID: "udid", completion: { result in
// Do whatever you need to do with the dictionary result
}
Also, I'd recommend you to change your NSDictionary with a swift Dictionary.
This is what the API returns as a response
{
AccessCode = 00000000;
Client = "0000 - My Company Name";
EmailAddress = "brandon#brandonthomas.me";
FirstName = Brandon;
Found = 1;
LastName = Thomas;
Status = A;
UDIDregistered = 1;
}
And this is what calls the function. I am calling at after clicking a button after an access code is being entered in a text field.
#IBAction func StartWizard(_ sender: UIButton) {
//Store entered access code
let accessCode = AccessCodeField.text!
var accessCodeFound: Bool? = nil
//Call API to validate Access Code
getResponse(accessCode:accessCode, UDID:myDeviceUDID) { result in
accessCodeFound = result["Found"] as! Bool
print("Value of Found during function:")
print(accessCodeFound)
//accessCodeFound = true
}
//If access code is valid, go to License view
print("Value of Found after function:")
print(accessCodeFound)
//accessCodeFound = nil ???
//it seems the value is getting reset after the function completes
if accessCodeFound == true{
//Load License View
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let licenseController = storyboard.instantiateViewController(identifier: "LicenseViewPanel")
self.show(licenseController, sender: Any?.self)
}
}

Function runs twice if nested async calls are executed and once otherwise. Need help pre-determining when this will happen

func handleGetAllPhotoURLs is called from the line below and I have confirmed that the line of code only executes once with breakpoints.
_ = FlickrClient.getAllPhotoURLs(currentPin: self.currentPin, fetchCount: fetchCount, completion: self.handleGetAllPhotoURLs(pin:urls:error:))
According to output from my print statements, the function runs twice because it prints two lines of output if urls.count is non-zero. However, if urls.count is zero then I only get one print statement that states "urls.count ---> 0"
handleGetAllPhotoURLs ---> urls.count ---> 0 //this line is always printed
handleGetAllPhotoURLs ---> urls.count ---> 21 //this line is only printed if the urls parameter is not empty
func handleGetAllPhotoURLs(pin: Pin, urls: [URL], error: Error?){
print("handleGetAllPhotoURLs ---> urls.count ---> \(urls.count)")
let backgroundContext: NSManagedObjectContext! = dataController.backGroundContext
if let error = error {
print("func mapView(_ mapView: MKMapView, didSelect... \n\(error)")
return
}
let pinId = pin.objectID
backgroundContext.perform {
let backgroundPin = backgroundContext.object(with: pinId) as! Pin
backgroundPin.urlCount = Int32(urls.count)
try? backgroundContext.save()
}
for (index, currentURL) in urls.enumerated() {
URLSession.shared.dataTask(with: currentURL, completionHandler: { (imageData, response, error) in
guard let imageData = imageData else {return}
connectPhotoAndPin(dataController: self.dataController, currentPin: pin , data: imageData, urlString: currentURL.absoluteString, index: index)
}).resume()
}
}
In addition, I have a UILabel that only reveals itself when urls.count is zero and I only want to reveal it when urls is empty.
Right now, if urls is not empty, the app is very quickly flashing the empty message UILabel. Which now makes sense to me because print statement shows that urls array is temporarily empty.
Is there a way for me to determine to avoid flashing the empty message UILabel to user when urls.count is non-zero?
edit: Added code below based on request. The function below is called to obtain [URL] in completion handler. Then the completion handler is fed into:
func handleGetAllPhotoURLs(pin: Pin, urls: [URL], error: Error?)
class func getAllPhotoURLs(currentPin: Pin, fetchCount count: Int, completion: #escaping (Pin, [URL], Error?)->Void)-> URLSessionTask?{
let latitude = currentPin.latitude
let longitude = currentPin.longitude
let pageNumber = currentPin.pageNumber
let url = Endpoints.photosSearch(latitude, longitude, count, pageNumber).url
var array_photo_URLs = [URL]()
var array_photoID_secret = [[String: String]]()
var array_URLString = [String]()
var array_URLString2 = [String]()
var count = 0
let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard let dataObject = data, error == nil else {
DispatchQueue.main.async {
completion(currentPin, [], error)
}
return
}
do {
let temp = try JSONDecoder().decode(PhotosSearch.self, from: dataObject)
temp.photos.photo.forEach{
let tempDict = [$0.id : $0.secret]
array_photoID_secret.append(tempDict)
let photoURL = FlickrClient.Endpoints.getOnePicture($0.id, $0.secret)
let photoURLString = photoURL.toString
array_URLString.append(photoURLString)
getPhotoURL(photoID: $0.id, secret: $0.secret, completion: { (urlString, error) in
guard let urlString = urlString else {return}
array_URLString2.append(urlString)
array_photo_URLs.append(URL(string: urlString)!)
count = count + 1
if count == temp.photos.photo.count {
completion(currentPin, array_photo_URLs, nil)
}
})
}
completion(currentPin, [], nil)
return
} catch let conversionErr {
DispatchQueue.main.async {
completion(currentPin, [], conversionErr)
}
return
}
}
task.resume()
return task
}
In the do block, you are calling completion twice. Please see the correction,
do {
let temp = try JSONDecoder().decode(PhotosSearch.self, from: dataObject)
if temp.photos.photo.isEmpty == false {
temp.photos.photo.forEach{
let tempDict = [$0.id : $0.secret]
array_photoID_secret.append(tempDict)
let photoURL = FlickrClient.Endpoints.getOnePicture($0.id, $0.secret)
let photoURLString = photoURL.toString
array_URLString.append(photoURLString)
getPhotoURL(photoID: $0.id, secret: $0.secret, completion: { (urlString, error) in
guard let urlString = urlString else {return}
array_URLString2.append(urlString)
array_photo_URLs.append(URL(string: urlString)!)
count = count + 1
if count == temp.photos.photo.count {
completion(currentPin, array_photo_URLs, nil)
}
})
}
} else {
completion(currentPin, [], nil)
}
return
}

Array is null after setting data in it

I have a JSON request that gets data from the Darksky API, I get the data properly and it is showing on the screen. However, When i'm trying to set the data from the array I get from the JSON call in another array, it stays empty.
This is my code:
just declaring the array:
var mForecastArray = [Weather]()
this is the function that calls the API:
func getForecast(){
Weather.forecast(withLocation: "37.8267,-122.4233") { (arr) in
DispatchQueue.main.async {
self.mForecastArray = arr
self.mTodayWeather = arr[0]
self.mCollectionView.reloadData()
}
}
}
The weird part is that it does work, and the data do shows on screen, but still, mForecastArray seems null.
This is the API call itself:
static func forecast(withLocation location: String, completion: #escaping ([Weather]) -> ()){
let url = basePath + location
let request = URLRequest(url: URL(string: url)!)
let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
var forecastArray: [Weather] = []
if let data = data{
do{
if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String:Any]{
if let dailyForecast = json["daily"] as? [String:Any]{
if let dailyData = dailyForecast["data"] as? [[String:Any]]{
for dataPoint in dailyData{
if let weatherObject = try? Weather(json: dataPoint){
forecastArray.append(weatherObject)
}
}
}
}
}
}catch{
print(error.localizedDescription)
}
completion(forecastArray)
}
}
task.resume()
}
It's a visual asynchronous illusion.
The static method forecast works asynchronously.
Most likely your code looks like
getForecast()
print(self.mForecastArray)
This cannot work because the array is populated much later.
Move the print line into the completion handler of the static method
func getForecast(){
Weather.forecast(withLocation: "37.8267,-122.4233") { (arr) in
DispatchQueue.main.async {
self.mForecastArray = arr
print(self.mForecastArray)
self.mTodayWeather = arr[0]
self.mCollectionView.reloadData()
}
}
}

How to wait for Swift's URLSession to finish before running again?

Probably a stupid question, but I'm a beginner at this.
The below code is supposed to get book information from Google Books from a keyword search. It then goes through the results and checks if I have a matching ISBN in a Firebase database. It works, but currently can only search 40 books as that's the Google Books API maximum per search.
Fortunately, I can specify where to start the index and get the next 40 books to search as well. Unfortunately, I've been trying for hours to understand how the URLSession works. All the methods I've tried have shown me that the code after the URLSession block doesn't necessarily wait for the session to complete. So if I check if I've found any matches afterward, it might not even be done searching.
I suspect the answer is in completion handling, but my attempts so far have been unsuccessful. Below is my code with a URL setup to take various starting index values.
var startingIndex = 0
//encode keyword(s) to be appended to URL
let query = query.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
let url = "https://www.googleapis.com/books/v1/volumes?q=\(query)&&maxResults=40&startIndex=\(startingIndex)"
URLSession.shared.dataTask(with: URL(string: url)!) { (data, response, error) in
if error != nil {
print(error!.localizedDescription)
}else{
let json = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String: AnyObject]
if let items = json["items"] as? [[String: AnyObject]] {
//for each result make a book and add title
for item in items {
if let volumeInfo = item["volumeInfo"] as? [String: AnyObject] {
let book = Book()
//default values
book.isbn13 = "isbn13"
book.isbn10 = "isbn10"
book.title = volumeInfo["title"] as? String
//putting all authors into one string
if let temp = volumeInfo["authors"] as? [String] {
var authors = ""
for i in 0..<temp.count {
authors = authors + temp[i]
}
book.author = authors
}
if let imageLinks = volumeInfo["imageLinks"] as? [String: String] {
book.imageURL = imageLinks["thumbnail"]
}
//assign isbns
if let isbns = volumeInfo["industryIdentifiers"] as? [[String: String]] {
for i in 0..<isbns.count {
let firstIsbn = isbns[i]
if firstIsbn["type"] == "ISBN_10" {
book.isbn10 = firstIsbn["identifier"]
}else{
book.isbn13 = firstIsbn["identifier"]
}
}
}
//adding book to an array of books
myDatabase.child("listings").child(book.isbn13!).observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists() {
if listings.contains(book) == false{
listings.append(book)
}
DispatchQueue.main.async { self.tableView.reloadData() }
}
})
myDatabase.child("listings").child(book.isbn10!).observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists() {
if listings.contains(book) == false{
listings.append(book)
}
DispatchQueue.main.async { self.tableView.reloadData() }
}
})
}
}
}
}
SVProgressHUD.dismiss()
}.resume()
Below is my revised code:
func searchForSale(query: String, startingIndex: Int) {
directionsTextLabel.isHidden = true
tableView.isHidden = false
listings.removeAll()
DispatchQueue.main.async { self.tableView.reloadData() }
SVProgressHUD.show(withStatus: "Searching")
//clear previous caches of textbook images
cache.clearMemoryCache()
cache.clearDiskCache()
cache.cleanExpiredDiskCache()
let url = "https://www.googleapis.com/books/v1/volumes?q=\(query)&&maxResults=40&startIndex=\(startingIndex)"
URLSession.shared.dataTask(with: URL(string: url)!) { (data, response, error) in
if error != nil {
print(error!.localizedDescription)
}else{
var needToContinueSearch = true
let json = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String: AnyObject]
if json["error"] == nil {
let totalItems = json["totalItems"] as? Int
if totalItems == 0 {
SVProgressHUD.showError(withStatus: "No matches found")
return
}
if let items = json["items"] as? [[String: AnyObject]] {
//for each result make a book and add title
for item in items {
if let volumeInfo = item["volumeInfo"] as? [String: AnyObject] {
let book = Book()
//default values
book.isbn13 = "isbn13"
book.isbn10 = "isbn10"
book.title = volumeInfo["title"] as? String
//putting all authors into one string
if let temp = volumeInfo["authors"] as? [String] {
var authors = ""
for i in 0..<temp.count {
authors = authors + temp[i]
}
book.author = authors
}
if let imageLinks = volumeInfo["imageLinks"] as? [String: String] {
book.imageURL = imageLinks["thumbnail"]
}
//assign isbns
if let isbns = volumeInfo["industryIdentifiers"] as? [[String: String]] {
for i in 0..<isbns.count {
let firstIsbn = isbns[i]
//checks if isbns have invalid characters
let isImproperlyFormatted = firstIsbn["identifier"]!.contains {".$#[]/".contains($0)}
if isImproperlyFormatted == false {
if firstIsbn["type"] == "ISBN_10" {
book.isbn10 = firstIsbn["identifier"]
}else{
book.isbn13 = firstIsbn["identifier"]
}
}
}
}
//adding book to an array of books
myDatabase.child("listings").child(book.isbn13!).observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists() {
if listings.contains(book) == false{
listings.append(book)
needToContinueSearch = false
}
DispatchQueue.main.async { self.tableView.reloadData() }
}
})
myDatabase.child("listings").child(book.isbn10!).observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists() {
if listings.contains(book) == false{
listings.append(book)
needToContinueSearch = false
}
DispatchQueue.main.async { self.tableView.reloadData() }
return
}
if startingIndex < 500 {
if needToContinueSearch {
let nextIndex = startingIndex + 40
self.searchForSale(query: query, startingIndex: nextIndex)
}
}
})
}
}
}
}else{
return
}
}
SVProgressHUD.dismiss()
}.resume()
//hide keyboard
self.searchBar.endEditing(true)
}
In your completion handler if any results have been returned you end with:
DispatchQueue.main.async { self.tableView.reloadData() }
to trigger reloading of your table with the updated information. At this same point is where you could determine of there may be more results and initiate the next asynchronous URL task. In outline your code might be:
let needToContinueSearch : Bool = ...;
DispatchQueue.main.async { self.tableView.reloadData() }
if needToContinueSearch
{ // call routine it initiate next async URL task
}
(If there is any reason to start the task from the main thread the if would be in the block.)
By not initiating the next search until after you've processed the results of the first you avoid having to deal with any issues of a subsequent callback trying to update your data at the same time as a previous one.
However if you find delaying the second search in this way is too slow you can investigate ways to overlap the operations, e.g. you might have the callback just pass the processing of the results to an async task on a serial queue (so that only one set of results is being processed at once) and initiate the next async URL task.
HTH
Declare a bool variable as isLoading and if that function is loading dont trigger urlsession. hope below sample will help you.
var isLoading : Bool = false
func loadMore(with pageCount: Int){
if isLoading { return }
isLoading = true
// call the network
URLSession.shared.dataTask(with: URL(string: "xxxxx")!) { (data, response, error) in
// after updating the data set isloding to false again
// do the api logic here
//
DispatchQueue.main.async {
// self.items = downloadedItems
self.tableView.reloadData()
self.isLoading = false
}
}.resume()
}

Creating and assigning a new NSManagedObject to a new NSManagedObject *sometimes* fails

I am creating a new NSManagedObject called "translation". Within the translation I need to create two additional NSManagedObjects called "phrase". Sometimes one of the phrase assignments will throw an error, but when I inspect the values they all look like they were created just fine. What gives???
Creating A Translation Object:
func getOrCreateTranslation(package: Package?, data: NSDictionary) -> Translation {
let translationId = data["id"] as! NSNumber
if let translation = self.getTranslation(translationId) {
return translation
} else {
let context = LocalDataStorage().context
let translation = NSEntityDescription.insertNewObjectForEntityForName("Translation", inManagedObjectContext: context) as! Translation
translation.id = translationId
let fromPhrase = data["from_phrase"]! as! NSDictionary
let toPhrase = data["to_phrase"]! as! NSDictionary
let pm = PhraseManager()
//*******
// *SOMETIMES* ONE OF THESE LINES FAIL WITH BAD_EXC_ACCESS code=1
translation.fromPhrase = pm.getOrCreatePhrase(fromPhrase)
translation.toPhrase = pm.getOrCreatePhrase(toPhrase)
//******
if package != nil {
package!.addTranslationObject(translation)
}
return translation
}
}
Creating A Phrase Object:
func getOrCreatePhrase(data: NSDictionary) -> Phrase {
// check if phrase exists
let phraseId = data["id"] as! NSNumber
if let phrase = self.getPhrase(phraseId) {
return phrase
} else {
let context = localDataStorage.context
let lm = LanguageManager()
let phrase = NSEntityDescription.insertNewObjectForEntityForName("Phrase", inManagedObjectContext: context) as! Phrase
phrase.id = phraseId
phrase.text = data["text"] as! String
phrase.audioUrl = data["audio_url"] as? String
let code = data["language"]!["language_code"] as! String
phrase.language = lm.getLanguageFromCode(code)
return phrase
}
}
Call Made to API:
func getPackageTranslations(package: Package, completion: ([Translation])-> Void) {
let currentLanguage: Language = LanguageManager().getCurrentLanguage()!
let urlString = baseAPIString + "/groups/\(package.id!)/translations/?language_code=\(currentLanguage.code)"
let session = NSURLSession.sharedSession()
let serachUrl = NSURL(string: urlString)
let task = session.dataTaskWithURL(serachUrl!) {
(data, response, error) -> Void in
if error != nil {
print(error?.localizedDescription)
} else {
let jsonData: NSDictionary!
do {
jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as! NSDictionary
} catch _ {
jsonData = NSDictionary()
}
let groupTranslationsData = jsonData["group_translations"] as! [NSDictionary]
var translations = [Translation]()
let context = LocalDataStorage().context
for groupTranslation in groupTranslationsData {
let translationData = groupTranslation["translation"] as! NSDictionary
let translation = TranslationManager().getOrCreateTranslation(package, data: translationData)
if translation.packages?.containsObject(package) == false {
//package.addTranslationObject(translation!)
//translation!.addPackageObject(package)
}
translations.append(translation)
}
do {
try context.save()
} catch {
print("There was a problem saving translation ")
}
dispatch_async(dispatch_get_main_queue(), {
completion(translations)
})
}
}
task.resume()
}
CoreData Context Class:
class LocalDataStorage {
let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context: NSManagedObjectContext!
init() {
context = appDelegate.managedObjectContext
}
}
This issue occurs when you create a NSManagedObjectContext with a concurrency pattern that it should be interacted on and you perform actions on the it on a thread different from the concurrency pattern specified during its initialization.
The completion block of NSURLSession.dataTaskWithURL is run on another thread, so you must dispatch to the type of thread specified in the context creation to perform any operation successfully on it.
If the concurrency type of your context is MainQueueConcurrencyType which is used in most cases, you must perform the context save method on the main queue.
dispatch_async(dispatch_get_main_queue()) {
do {
try context.save()
} catch {
print("There was a problem saving translation ")
}
completion(translations)
}