FirebaseStorage Pod Version (1.0.1)
var storageRef: FIRStorageReference! = FIRStorage.storage().reference()
let uploadTask = storageRef.putData(UIImagePNGRepresentation(image)!, metadata: nil) { metadata, error in
if (error != nil) {
} else {
let downloadURL = metadata!.downloadURL
print(downloadURL)
}
}
I simply selected a image from ImagePicker and tried to save Image data to the firebase storage. As illustrated in firebase docs.(Docs Reference) Upload from data in memory section.
It's throwing following error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]'
*** First throw call stack:
(
0 CoreFoundation 0x02025494 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x03eb2e02 objc_exception_throw + 50
2 CoreFoundation 0x01f0c6d2 -[__NSPlaceholderDictionary initWithObjects:forKeys:count:] + 386
3 CoreFoundation 0x01f2095b +[NSDictionary dictionaryWithObjects:forKeys:count:] + 75
4 FirePlay 0x001e42bc -[FIRStorageUploadTask enqueue] + 815
5 FirePlay 0x001e010c -[FIRStorageReference putData:metadata:completion:] + 880
The same code used to work before. I am not sure what's the problem now.
Turns out that we can't put files in root storage node. need to create child node and then only you can save the file. It was clearly mentioned in the Firebase docs somehow I overlooked it.
E.g
let mountainsRef = storageRef.child("mountains.jpg")
let uploadTask = mountainsRef.putData(UIImagePNGRepresentation(image)!, metadata: nil) { metadata, error in
if (error != nil) {
} else {
let downloadURL = metadata!.downloadURL
print(downloadURL)
}
}
Related
I have this error after saving entites (DictionaryMarketSubcategoryEntity), than saving them again.
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<DictionaryMarketSubcategoryEntity 0x60000269e2b0> setValue:forUndefinedKey:]: the entity DictionaryMarketSubcategoryEntity is not key value coding-compliant for the key "(null)".'
*** First throw call stack:
(
0 CoreFoundation 0x00007ff8004278cb __exceptionPreprocess + 242
1 libobjc.A.dylib 0x00007ff80004dba3 objc_exception_throw + 48
2 CoreFoundation 0x00007ff80042753d -[NSException init] + 0
3 CoreData 0x00007ff8049b8a02 -[NSMergePolicy _mergeToManyUnionRelationshipsForObject:andObject:] + 945
4 CoreData 0x00007ff8049bcf29 -[NSMergePolicy resolveConstraintConflicts:error:] + 5412
5 CoreData 0x00007ff8049bb99a -[NSMergePolicy resolveConflicts:error:] + 210
6 CoreData 0x00007ff80497a534 -[NSManagedObjectContext save:] + 3215
7 FCDev 0x0000000101624e04 $s5FCDev13CoreDataStackC20saveContextIfChangedyyF + 1524
8 FCDev 0x0000000101573f8b $s5FCDev36CoreDataStorageServiceImplementationC5writeyyF + 43
9 FCDev 0x0000000101575710 $s5FCDev36CoreDataStorageServiceImplementationCAA0dE0A2aDP5writeyyFTW + 16
10 FCDev 0x0000000100a02291
Here is entity (DictionaryMarketSubcategoryEntity) that I save. It has child subcategories (DictionaryMarketSubcategoryEntity). If I comment and remove child, then everything OK. It seems that when I save array of DictionaryMarketCategoryEntity it save child entities (DictionaryMarketSubcategoryEntity) and after resaving them, the merge policy give a crash. Why?
Here is code of init:
extension DictionaryMarketCategoryEntity: JSONInitializable {
convenience init(json: JSON) {
self.init(context: AppInstances.shared.storage.context)
self.backgroundColor = json["backgroundColor"].stringValue
self.backgroundImagePath = json["backgroundImage"].stringValue
self.descriptionText = json["description"].stringValue
self.icon = json["icon"].stringValue
self.id = json["id"].stringValue
self.imagePath = json["image"].stringValue
self.parent = json["parent"].stringValue
self.span = json["span"].int16Value
self.title = json["title"].stringValue
self.type = json["type"].stringValue
let childrenJson = json["children"].arrayValue
let childrenEntities = childrenJson.map { DictionaryMarketSubcategoryEntity(json: $0) }
let childrenSet = NSSet(array: childrenEntities)
addToChildren(childrenSet)
}
}
extension DictionaryMarketSubcategoryEntity {
convenience init(json: JSON) {
self.init(context: AppInstances.shared.storage.context)
self.id = json["id"].stringValue
self.title = json["title"].stringValue
self.descriptionText = json["description"].stringValue
self.imagePath = json["image"].stringValue
self.backgroundImagePath = json["backgroundImage"].stringValue
self.backgroundColor = json["backgroundColor"].stringValue
self.icon = json["icon"].stringValue
self.parent = json["parent"].stringValue
self.span = json["span"].int16Value
self.type = json["type"].stringValue
}
}
I've added the parent relationship in subcategory. Then in main entity using addToChildrenEntities - the autogenerated by xcode function. After that the error has gone. It's not because the JSON. It seems that core data fail to merge if child entity (subcategory) doesn't know what is his parent entity.
I’m new to iOS (Swift) development, and I’ve been tinkering with this all week. I am trying to update only the city of the postal addresses of a contact from iOS Contacts, and leave the rest of the postal address (street, ZIP code, etc.) intact. I figured I could just get a mutable copy of the contact.postalAddress using postalAddress.mutableCopy(), and then write to mutableCopy.city = “New City”. However, when I try this I get a -[CNLabeledValue mutableCopyWithZone:]: unrecognized selector sent to instance error. My current solution is to create a new address filled with values from the old address, but it seems there should be a better way (like copying the old address and just changing the city, like I’m trying to do). Is there something I’m missing? Thanks!
So, what I want to work:
func updateContact(contact: CNContact) -> BooleanLiteralType{
var success = false
let store = CNContactStore()
do {
let updContact = contact.mutableCopy() as! CNMutableContact
var addrArray = [CNLabeledValue<CNPostalAddress>]()
for postAddress in updContact.postalAddresses{
let curLabel = postAddress.label
var address = postAddress.mutableCopy() as! CNMutablePostalAddress
address.city = "Cityville"
let curAddrLabeledVal = CNLabeledValue<CNPostalAddress>(label:curLabel, value:address)
addrArray.append(curAddrLabeledVal)
}
updContact.postalAddresses = addrArray
let req = CNSaveRequest()
req.update(updContact)
try store.execute(req)
success = true
}
catch let err{
print(err)
}
return success
}
This results in the following exception:
2020-05-26 00:06:19.537517+0200 My App[78089:4168944] -[CNLabeledValue mutableCopyWithZone:]: unrecognized selector sent to instance 0x600001388b80
2020-05-26 00:06:19.545537+0200 My App[78089:4168944] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CNLabeledValue mutableCopyWithZone:]: unrecognized selector sent to instance 0x600001388b80'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff23e3cf0e __exceptionPreprocess + 350
1 libobjc.A.dylib 0x00007fff50ba89b2 objc_exception_throw + 48
2 CoreFoundation 0x00007fff23e5dc34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x00007fff23e4190c ___forwarding___ + 1436
4 CoreFoundation 0x00007fff23e43bf8 _CF_forwarding_prep_0 + 120
5 My App 0x00000001015b4898 $s12My_App13updateContact7contactSbSo9CNContactC_tF + 1048
6 My App 0x00000001015bf3f1 $s12My_App11ContentViewV4bodyQrvg7SwiftUI05TupleD0VyAE0D0PAEE7opacityyQrSdFQOyAiEE5frame5width6height9alignmentQr12CoreGraphics7CGFloatVSg_ArE9AlignmentVtFQOyAA11ProgressBarV_Qo__Qo__AE010NavigationD0VyAiEE010navigationS5Items7leading8trailingQrqd___qd_0_tAeHRd__AeHRd_0_r0_lFQOyAiEE0uS5TitleyQrAE4TextVFQOyAE4ListVys5NeverOAE7ForEachVySaySo9CNContactCGSSAE6HStackVyAE0T4LinkVyAA10ContactRowVAA13ContactDetailVGGGG_Qo__AE6ButtonVyA4_GA29_Qo_GtGyXEfU_A30_yXEfU_yycfU2_yycfU_ + 465
7 My App 0x00000001015bf8f0 $sIeg_IeyB_TR + 48
8 libdispatch.dylib 0x0000000101877f11 _dispatch_call_block_and_release + 12
9 libdispatch.dylib 0x0000000101878e8e _dispatch_client_callout + 8
10 libdispatch.dylib 0x000000010187b2d8 _dispatch_queue_override_invoke + 1022
11 libdispatch.dylib 0x000000010188a399 _dispatch_root_queue_drain + 351
12 libdispatch.dylib 0x000000010188aca6 _dispatch_worker_thread2 + 135
13 libsystem_pthread.dylib 0x00007fff51c089f7 _pthread_wqthread + 220
14 libsystem_pthread.dylib 0x00007fff51c07b77 start_wqthread + 15
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
This is the solution that works, but as you can see it’s not really what I want as it requires me to define all postalAddress attributes:
func updateContact(contact: CNContact) -> BooleanLiteralType{
var success = false
let store = CNContactStore()
do {
let updContact = contact.mutableCopy() as! CNMutableContact
var addrArray = [CNLabeledValue<CNPostalAddress>]()
for postAddress in updContact.postalAddresses{
let curLabel = postAddress.label
let address = CNMutablePostalAddress()
address.street = postAddress.value.street
address.city = "Cityville"
address.postalCode = postAddress.value.postalCode
address.state = postAddress.value.state
address.country = postAddress.value.country
address.isoCountryCode = postAddress.value.isoCountryCode
address.subAdministrativeArea = postAddress.value.subAdministrativeArea
address.subLocality = postAddress.value.subLocality
let curAddrLabeledVal = CNLabeledValue<CNPostalAddress>(label:curLabel, value:address)
addrArray.append(curAddrLabeledVal)
}
updContact.postalAddresses = addrArray
let req = CNSaveRequest()
req.update(updContact)
try store.execute(req)
success = true
}
catch let err{
print(err)
}
return success
}
Any help is much appreciated!
We are experiencing an exception in our project:
2019-08-08 10:18:28.703708-0600 AppName[99385:5069475] *** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: !destNodeMixerConns.empty() && !isDestNodeConnectedToIONode'
*** First throw call stack:
(
0 CoreFoundation 0x000000010ee2e8db __exceptionPreprocess + 331
1 libobjc.A.dylib 0x000000010e21bac5 objc_exception_throw + 48
2 CoreFoundation 0x000000010ee2e662 +[NSException raise:format:arguments:] + 98
3 AVFAudio 0x0000000111b94dbc _Z19AVAE_RaiseExceptionP8NSStringz + 156
4 AVFAudio 0x0000000111bf3937 _Z11_AVAE_CheckPKciS0_S0_b + 295
5 AVFAudio 0x0000000111b8cb8f _ZN18AVAudioEngineGraph8_ConnectEP19AVAudioNodeImplBaseS1_jjP13AVAudioFormat + 1031
6 AVFAudio 0x0000000111bfb598 _ZN17AVAudioEngineImpl7ConnectEP11AVAudioNodeS1_mmP13AVAudioFormat + 194
7 AVFAudio 0x0000000111bfb5ff -[AVAudioEngine connect:to:format:] + 83
8 AppName 0x000000010a424c10 $s8AudioKitAAC6outputAA6AKNodeCSgvWZTf4dd_n + 2576
9 AppName 0x000000010a4230fd $s8AudioKitAAC6outputAA6AKNodeCSgvsZ + 93
10 AppName 0x000000010a2ba3a3 $s6AppName7MaestroC17setUpTrackPlayers7fileURLy10Foundation0H0V_tF + 1235
Examining the common gotchas video I see a similar exception being thrown, required condition is false: !nodeMixerConns.empty() && !hasDirectConnToIONode, which is caused by allowing the variables to go out of scope rather than be retained in the class.
So this occurs when we create an AKPlayer, which is retained in a class, then we create an AKTimePitch with this player which is also retained in that class, and finally assign that to AudioKit.output which triggers the exception. After that we were storing the class that holds onto the player and time pitch in an array, so I tried to move this up after it's created in hopes that was the issue, but I see the same exception.
Interestingly, this code works fine initially when we load up the first song but crashes when we hit the next button to load up the next song.
final class Maestro : NSObject {
static let shared = Maestro()
var audioPlayers = [TrackPlayer]()
func setUpTrackPlayers(fileURL: URL) {
let playerOne = TrackPlayer(url: fileURL)
audioPlayers.append(playerOne)
AudioKit.output = playerOne.handleMixerChain() //boom
do {
try AudioKit.start()
} catch {
print("Maestro AudioKit.start error: \(error)")
}
}
func next() {
for player in audioPlayers {
player.stop()
}
audioPlayers.removeAll()
setUpTrackPlayers(fileURL: newSong.getFileUrl())
}
}
final class TrackPlayer {
let player : AKPlayer
lazy var timePitch = AKTimePitch()
init(url: URL) {
player = AKPlayer(url: url)!
}
func handleMixerChain(pitch: Double = 0.0, tempo: Double = 1.0) -> AKTimePitch {
timePitch = AKTimePitch(player)
timePitch.pitch = pitch
timePitch.rate = tempo
return timePitch
}
}
Any ideas? If you need any more info let me know. May be good to note we are updating from AudioKit 4.5.5 where we didn't experience this crash.
I've opened the project and it seems like you're being a bit too relaxed about your set up / tear down of the signal chain. You're setting up AudioKit's output using local variables, never tearing down this signal chain, and then coming back resetting the AudioKit output and telling AudioKit to start without ever calling AudioKit.stop().
Here are three functions from my code:
func workOutResults() {
var fixtures = [Fixture]()
let fixtureFetch: NSFetchRequest<Fixture> = Fixture.fetchRequest()
do {
fixtures = try coreDataStack.managedContext.fetch(fixtureFetch)
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
for fixture in fixtures {
// do stuff here
}
}
func processUpdates() {
// relevant code snippet below
let theTable = loadLeagueTableFor(leagueName: "Championship", cds: cds)
}
func loadLeagueTableFor(leagueName: String, cds: CoreDataStack) -> [Club] {
var leagueArray = [Club]()
// Set up the sort descriptors
let pointsSortDescriptor: NSSortDescriptor = {
let compareSelector = #selector(NSString.localizedStandardCompare(_:))
return NSSortDescriptor(key: #keyPath(Club.leaguePoints),
ascending: false,
selector: compareSelector)
}()
let goalDiffSortDescriptor: NSSortDescriptor = {
let compareSelector = #selector(NSString.localizedStandardCompare(_:))
return NSSortDescriptor(key: #keyPath(Club.leagueGoalDiff),
ascending: false,
selector: compareSelector)
}()
let goalsForSortDescriptor: NSSortDescriptor = {
let compareSelector = #selector(NSString.localizedStandardCompare(_:))
return NSSortDescriptor(key: #keyPath(Club.leagueGoalsFor),
ascending: false,
selector: compareSelector)
}()
let clubNameSortDescriptor: NSSortDescriptor = {
let compareSelector = #selector(NSString.localizedStandardCompare(_:))
return NSSortDescriptor(key: #keyPath(Club.name),
ascending: true,
selector: compareSelector)
}()
// Do the Fetch request of Clubs, placing them in order into leagueArray
let clubFetch: NSFetchRequest<Club> = Club.fetchRequest()
clubFetch.predicate = NSPredicate(format: "%K == %#", #keyPath(Club.league.nameID), leagueName)
clubFetch.sortDescriptors = [pointsSortDescriptor, goalDiffSortDescriptor, goalsForSortDescriptor, clubNameSortDescriptor]
do {
leagueArray = try cds.managedContext.fetch(clubFetch)
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
cds.saveContext()
return leagueArray
}
If I call...
workOutResults()
coreDataStack.saveContext()
processUpdates()
... everything works fine. Yet if I call...
workOutResults()
processUpdates()
... I get the following error in loadLeagueTableFor():
2018-01-17 19:37:38.228954+0000 Tycoon[1331:32725] -[__NSCFNumber localizedStandardCompare:]: unrecognized selector sent to instance 0xb000000000000022
2018-01-17 19:37:38.278901+0000 Tycoon[1331:32725] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber localizedStandardCompare:]: unrecognized selector sent to instance 0xb000000000000022'
*** First throw call stack:
(
0 CoreFoundation 0x0000000104da212b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x0000000103d6ef41 objc_exception_throw + 48
2 CoreFoundation 0x0000000104e23024 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x0000000104d24f78 ___forwarding___ + 1432
4 CoreFoundation 0x0000000104d24958 _CF_forwarding_prep_0 + 120
5 Foundation 0x00000001037aaad2 _NSCompareObject + 46
6 Foundation 0x0000000103806097 _NSSortFunctionMany + 674
7 CoreFoundation 0x0000000104d1b3bc __CFSimpleMergeSort + 124
8 CoreFoundation 0x0000000104d1b41c __CFSimpleMergeSort + 220
9 CoreFoundation 0x0000000104d1b41c __CFSimpleMergeSort + 220
10 CoreFoundation 0x0000000104d1b41c __CFSimpleMergeSort + 220
11 CoreFoundation 0x0000000104d1b2fb CFSortIndexes + 827
12 CoreFoundation 0x0000000104d51726 CFMergeSortArray + 454
13 Foundation 0x00000001037aa81e _sortedObjectsUsingDescriptors + 596
14 Foundation 0x00000001037aa570 -[NSArray(NSKeyValueSorting) sortedArrayUsingDescriptors:] + 531
15 CoreData 0x000000010477909e -[NSManagedObjectContext executeFetchRequest:error:] + 4590
16 libswiftCoreData.dylib 0x00000001045e8f1a )
libc++abi.dylib: terminating with uncaught exception of type NSException
So saving the context in between the two fetches avoids the crash, but poses a problem as it means I have to save in a place I would rather not. Any idea why I am getting the error?
It's unclear why this would ever work, because you're comparing numeric values with compareSelector, which you define to be a method on NSString. The error message describes this exactly-- you're trying to compare two numbers using a method that doesn't exist for numbers.
When using sort descriptors, the selector and comparator versions of the constructors are only necessary if you don't want the common sorting implied by the value of ascending. In the case of numeric values, if you simply want them sorted by value, you don't need either. You can simply use something like
NSSortDescriptor(key: #keyPath(Club.leaguePoints),
ascending: false)
When my code tries to download the series from the API, it randomly crashes with this error message:
(entity: Series; id:
0x7b181002016-04-04 14:01:33.868 Postzegel Catalogus[1816:39059]
CoreData: error: Serious application error. Exception was caught
during Core Data change processing. This is usually a bug within an
observer of NSManagedObjectContextObjectsDidChangeNotification.
-[NSCFSet addObject:]: attempt to insert nil with userInfo (null) 0 (entity: Series; id:
0x7b00c450
;
2016-04-04 14:01:33.871 Postzegel Catalogus[1816:39059] *** Terminating app due
to uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSCFSet addObject:]: attempt to insert nil'
*** First throw call stack: ( 0 CoreFoundation 0x0083d494 __exceptionPreprocess + 180 1 libobjc.A.dylib
0x02551e02 objc_exception_throw + 50 2 CoreFoundation
0x0083d3bd +[NSException raise:format:] + 141 3 CoreFoundation
0x0070c959 -[__NSCFSet addObject:] + 185 4 CoreData
0x0038a010 -[NSManagedObjectContext(_NSInternalChangeProcessing)
_processPendingInsertions:withDeletions:withUpdates:] + 560 5 CoreData 0x003846da
-[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] + 2410 6 CoreData 0x00383d56 -[NSManagedObjectContext processPendingChanges] + 54 7
CoreData 0x003ae5e4
-[NSManagedObjectContext(_NestedContextSupport) _parentProcessSaveRequest:inContext:error:] + 116 8 CoreData 0x00433bec __82-[NSManagedObjectContext(_NestedContextSupport)
executeRequest:withContext:error:]_block_invoke + 412 9 CoreData
0x003a924c internalBlockToNSManagedObjectContextPerform + 76 10
libdispatch.dylib 0x03c8f9cd
_dispatch_client_callout + 14 11 libdispatch.dylib 0x03c76d90 _dispatch_barrier_sync_f_slow_invoke + 133 12
libdispatch.dylib 0x03c8f9cd
_dispatch_client_callout + 14 13 libdispatch.dylib 0x03c74f7c _dispatch_main_queue_callback_4CF + 910 14 CoreFoundation
0x007871be __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE + 14 15
CoreFoundation 0x00745434 __CFRunLoopRun + 2356
16 CoreFoundation 0x00744846
CFRunLoopRunSpecific + 470 17 CoreFoundation
0x0074465b CFRunLoopRunInMode + 123 18 GraphicsServices
0x07a8d664 GSEventRunModal + 192 19 GraphicsServices
0x07a8d4a1 GSEventRun + 104 20 UIKit
0x0102beb9 UIApplicationMain + 160 21 Postzegel Catalogus
0x000f63b1 main + 145 22 libdyld.dylib
0x03cb9a25 start + 1 ) (entity: Series; id: 0x7b25bc40
;
And I just don't know why? I used a private Managed Context Option so that using a . I even set the fields in my .xcdatamodeld to optional. So it shouldn't be a problem it it is nil? And it just keeps crashing randomly, not even at the same object. How can I fix this?
I included my code in the hope that might help you. I have removed my API key, so you won't be able to try it out. If you have any other comment on my code please tell me, I'm new to Core Data and Alamofire so I have the tenancy to make 'spaghetti code'.
Thanks in advance
import Foundation
import CoreData
import Alamofire
import SwiftyJSON
//CoreData Init
let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext: NSManagedObjectContext = appDelegate.managedObjectContext
let queue = dispatch_queue_create("com.GJ-Computers.Postzegel-Catalogus.responseJSON-Manager", DISPATCH_QUEUE_CONCURRENT)
//Colnect API
let LANG: String = NSLocale.preferredLanguages()[0].substringToIndex(NSLocale.preferredLanguages()[0].startIndex.advancedBy(2))
let DATE = NSCalendar.currentCalendar().component([.Day, .Month, .Year], fromDate: NSDate())
let API_KEY: String = "----" //Private API KEY
let CAT_STAMPS: String = ("cat/stamps/")
var BASE_URL: String{
return ("http://api.colnect.net/" + LANG + "/api/" + API_KEY + "/")
}
//Ghetto Delegate
var didGetCountires: Bool = false
var didGetYears: Bool = false
var didGetSeries: Bool = false
//MARK: - First Time setup Database
func setupDatabase(){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)){
getYears() //Download Years per country from database
}
}
//get Series
func getSeries(){
//Retrieve Countries from Coredata
let countryFetchRequest = NSFetchRequest(entityName: "Countries")
var results: [Countries]?
do {
results = try managedContext.executeFetchRequest(countryFetchRequest) as? [Countries]
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
let resultCount = results!.count
var completedRequestCount: Int = 0
var requestedRequests = 0
for result in results!{
let countryID = result.countryID
Alamofire.request(.GET, (BASE_URL + "series/"+CAT_STAMPS+"producer/\(countryID)")).responseJSON(queue: queue, completionHandler:{ response in
if let json = response.result.value{
let privateMOC = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
privateMOC.parentContext = managedContext
let rawData = JSON(json)
for data in rawData {
//Setup let
let seriesID = Int(data.1.array![0].string!)
let seriesName = data.1.array![1].string
let itemCount = Int(data.1.array![2].string!)
if seriesID != 0 && itemCount != 0 && seriesName != nil{
privateMOC.performBlock{
let series = NSEntityDescription.insertNewObjectForEntityForName("Series", inManagedObjectContext: managedContext) as! Series
series.countryID = countryID
series.seriesID = seriesID
series.seriesName = seriesName
series.itemCount = itemCount
print(completedRequestCount)
do {
try privateMOC.save()
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
}
}else{
print("ERROR")
}
}
}
completedRequestCount += 1
print(completedRequestCount)
})
requestedRequests += 1
if(requestedRequests == resultCount){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)){
while(true){
if(completedRequestCount == resultCount){
didGetSeries = true
sleep(3)
print("DEBUG - Series Done")
break
}
}
}
}
}
}
Actually, the reason, using private MOC solved your problem is because you are performing the operation in the background thread, and when you do coredata operation in the background thread you have to ensure that
Managed object contexts are bound to the thread (queue) that they are
associated with upon initialization
Managed objects retrieved from a context are bound to the same queue
that the context is bound to
FYI not because of ("I made a private MOC, but when you make one you have to consistently add it in the code otherwise it won't work. I forgot to replace managedContext with privateMOC")
Solved it Myself. I made a private MOC, but when you make one you have to consistently add it in the code otherwise it won't work. I forgot to replace managedContext with privateMOC. My bad
let series = NSEntityDescription.insertNewObjectForEntityForName("Series", inManagedObjectContext: managedContext) as! Series
should be
let series = NSEntityDescription.insertNewObjectForEntityForName("Series", inManagedObjectContext: privateMOC) as! Series