CoreStore icon indicating copy to clipboard operation
CoreStore copied to clipboard

Question: query works but Relationship objects empty

Open nneuberger1 opened this issue 3 years ago • 5 comments

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.. ;-)

nneuberger1 avatar Mar 18 '21 23:03 nneuberger1

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 avatar Mar 18 '21 23:03 JohnEstropia

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 avatar Mar 19 '21 03:03 nneuberger1

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 avatar Mar 19 '21 03:03 nneuberger1

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

nneuberger1 avatar Mar 19 '21 03:03 nneuberger1

@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 avatar Apr 13 '21 00:04 JohnEstropia