Question: query works but Relationship objects empty #359

Open
opened 2025-12-29 15:29:57 +01:00 by adam · 5 comments
Owner

Originally created by @nneuberger1 on GitHub (Mar 19, 2021).

I can query an a primary object based on the data of a secondary relationship object like:

let localMediaUserDetail = try dataStack.fetchOne(From().where(.$mediaData ~ .$identifier == "1000"))

But after retrieving the data, I get an nil object associated on that relationship.

if let mediaData = localMediaUserDetails.mediaData {
print("!--")
print("!-- mediaData")
print("!-- identifer: (mediaData.identifier)")
print("!-- title: (mediaData.title)")
}

The above is never hit.

Here's part of my configuration:

public class LocalMediaUserDetail: CoreStoreObject {

@Field.Relationship("mediaData", inverse: \.$mediaUserDetail)
var mediaData: LocalMediaData?

}

and the related class

public class LocalMediaData: CoreStoreObject {
// parent
@Field.Relationship("mediaUserDetail")
var mediaUserDetail: LocalMediaUserDetail?

@Field.Stored("identifier")
var identifier: String = ""

@Field.Stored("title")
var title: String = ""

}

Is there something I'm doing wrong?

My google query kung fu` has been exhausted so I posted the question.. ;-)

Originally created by @nneuberger1 on GitHub (Mar 19, 2021). I can query an a primary object based on the data of a secondary relationship object like: let localMediaUserDetail = try dataStack.fetchOne(From<LocalMediaUserDetail>().where(\.$mediaData ~ \.$identifier == "1000")) But after retrieving the data, I get an nil object associated on that relationship. if let mediaData = localMediaUserDetails.mediaData { print("!--") print("!-- mediaData") print("!-- identifer: \(mediaData.identifier)") print("!-- title: \(mediaData.title)") } The above is never hit. Here's part of my configuration: public class LocalMediaUserDetail: CoreStoreObject { @Field.Relationship("mediaData", inverse: \.$mediaUserDetail) var mediaData: LocalMediaData? } and the related class public class LocalMediaData: CoreStoreObject { // parent @Field.Relationship("mediaUserDetail") var mediaUserDetail: LocalMediaUserDetail? @Field.Stored("identifier") var identifier: String = "" @Field.Stored("title") var title: String = "" } Is there something I'm doing wrong? My google query kung fu` has been exhausted so I posted the question.. ;-)
Author
Owner

@JohnEstropia commented on GitHub (Mar 19, 2021):

Have you tried to query for just the relationship object? It's possible the object isn't getting saved (yet), maybe due to a transaction error or a timing issue

@JohnEstropia commented on GitHub (Mar 19, 2021): Have you tried to query for just the relationship object? It's possible the object isn't getting saved (yet), maybe due to a transaction error or a timing issue
Author
Owner

@nneuberger1 commented on GitHub (Mar 19, 2021):

Do you mean directly querying the relationship like this? I do get a record back. But possible error that might be a cause?

        let localMediaData = try dataStack.fetchOne(From<LocalMediaData>().where(\.$identifier == "1000"))

        if let local = localMediaData {
            print("!-- localMediaData found")
            
            if let detail = local.mediaUserDetail {
                print("!-- localMediaUserDetail found")
            }
            else {
                print("!-- localMediaUserDetail NOT found")
            }
            
        }
        else {
            print("!-- localMediaData NOT found")
        }

Results in:

!-- localMediaData found
2021-03-18 22:43:33.413047-0500 ThingsAboveIOS QA[24096:818105] [error] error: API Misuse: Attempt to serialize store access on non-owning coordinator (PSC = 0x600001b357a0, store PSC = 0x0)
CoreData: error: API Misuse: Attempt to serialize store access on non-owning coordinator (PSC = 0x600001b357a0, store PSC = 0x0)
!-- localMediaUserDetail NOT found

@nneuberger1 commented on GitHub (Mar 19, 2021): Do you mean directly querying the relationship like this? I do get a record back. But possible error that might be a cause? let localMediaData = try dataStack.fetchOne(From<LocalMediaData>().where(\.$identifier == "1000")) if let local = localMediaData { print("!-- localMediaData found") if let detail = local.mediaUserDetail { print("!-- localMediaUserDetail found") } else { print("!-- localMediaUserDetail NOT found") } } else { print("!-- localMediaData NOT found") } Results in: !-- localMediaData found 2021-03-18 22:43:33.413047-0500 ThingsAboveIOS QA[24096:818105] [error] error: API Misuse: Attempt to serialize store access on non-owning coordinator (PSC = 0x600001b357a0, store PSC = 0x0) CoreData: error: API Misuse: Attempt to serialize store access on non-owning coordinator (PSC = 0x600001b357a0, store PSC = 0x0) !-- localMediaUserDetail NOT found
Author
Owner

@nneuberger1 commented on GitHub (Mar 19, 2021):

If it helps, maybe I'm saving them improperly. I do get a success response on the completion.

dataStack.perform(
asynchronous: { (transaction) -> Void in

            print("!-- transaction starting...")
            let mediaUserDetail = transaction.create(Into<LocalMediaUserDetail>())

            // don't add the id.

            mediaUserDetail.identifier = UUID().uuidString

            let mediaData = transaction.create(Into<LocalMediaData>())
            mediaData.identifier = "1000"
            mediaData.title = "this is the media title"
            mediaUserDetail.mediaData = mediaData

            print("!-- transaction ending...")
        },
        completion: { (result) -> Void in
            print("!-- transaction completion...")

            switch result {
            case .success:
                print("!-- success!")
                
                self.findData()
                
            case .failure(let error): print("!-- error: \(error)")
            }
        }
    )
@nneuberger1 commented on GitHub (Mar 19, 2021): If it helps, maybe I'm saving them improperly. I do get a success response on the completion. dataStack.perform( asynchronous: { (transaction) -> Void in print("!-- transaction starting...") let mediaUserDetail = transaction.create(Into<LocalMediaUserDetail>()) // don't add the id. mediaUserDetail.identifier = UUID().uuidString let mediaData = transaction.create(Into<LocalMediaData>()) mediaData.identifier = "1000" mediaData.title = "this is the media title" mediaUserDetail.mediaData = mediaData print("!-- transaction ending...") }, completion: { (result) -> Void in print("!-- transaction completion...") switch result { case .success: print("!-- success!") self.findData() case .failure(let error): print("!-- error: \(error)") } } )
Author
Owner

@nneuberger1 commented on GitHub (Mar 19, 2021):

I'm using the SQLiteStore as well on setup of the dataStack.

@nneuberger1 commented on GitHub (Mar 19, 2021): I'm using the SQLiteStore as well on setup of the dataStack.
Author
Owner

@JohnEstropia commented on GitHub (Apr 13, 2021):

@nneuberger1 Apologies for going silent here, but have you sorted it out?
A quick google of the error

error: API Misuse: Attempt to serialize store access on non-owning coordinator

shows this is likely a multithreading issue. Are you sure your dataStack.fetch*() calls are running in the main queue?

@JohnEstropia commented on GitHub (Apr 13, 2021): @nneuberger1 Apologies for going silent here, but have you sorted it out? A quick google of the error > error: API Misuse: Attempt to serialize store access on non-owning coordinator shows this is likely a multithreading issue. Are you sure your `dataStack.fetch*()` calls are running in the main queue?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#359