Swift - Parse Platform - Error attempting to .pin() an object to local datastore. Old objectId is (null)

Viewed 74

I have two classes in my app.

class UserEvent: PFObject, PFSubclassing {
  @NSManaged var target:PFUser
  @NSManaged var notifyTarget:Bool
  @NSManaged var requiresAction:Bool
  @NSManaged var affectedRelationship:UserRelationship?
  static func parseClassName() -> String {
    return "UserEvent"
  }
}

and

class UserRelationship:PFObject, PFSubclassing {
  @NSManaged var userPair:[PFUser]
  @NSManaged var userA:PFUser
  @NSManaged var userB:PFUser
  @NSManaged var aFollowingB:Bool
  @NSManaged var bFollowingA:Bool
  static func parseClassName() -> String {
    return "UserRelationship"
  }
}

I am attempting to pin() a new instance of UserEvent, which contains a pointer to the affected UserRelationship.

I am aware that pinning in Parse is recursive, and that when I pin an object, it will also pin any object pointed to by that object. The issue I'm having is that whenever I attempt to pin a new UserEvent, it also tries to recursively pin the linked UserRelationship object, which was previously pinned in code long past. In doing so, I get the following error:

2020-06-03 11:54:33.890788-0500 SwagLeagues[1848:46123] [Error]: Attempted to change an objectId to one that's already known to the OfflineStore. className: UserRelationship old: (null), new: UandhrZsEO

I'm assuming that is the issue anyway. I'm not 100% sure, as the error message provides a (null) objectId for the existing pinned object.

I've read the parse docs on pinning over and over, and I'm not sure how to handle this situation. I've tried using .pin(withName:) on the UserRelationship object first, and then using .pin(withName:) on the UserEvent object. I know this is not the answer though as the parse docs specifically state:

"There are a couple of side effects of enabling the local datastore that you should be aware of. When enabled, there will only be one instance of any given PFObject."

Here is the question: How can I pin my newly created UserEvent object, if the linked affectedRelationship has already been pinned? Do I have to unpin the UserRelationship object, before I attempt to pin the UserEvent object? Thats seems a bit silly to me. Thanks.

EDIT 1: Ok this is getting weird. In doing more research as to why my error message was show (null) for the old objectId I've found something out...

I ran a simple test where I tried to create a single UserRelationship object, save it, and .pin() it. It's still giving me the error. The logic is as follows:

  1. Create the new UserRelationship() object
  2. Save the new UserRelationship() object in the background
  3. The creation of the new UserRelationship() triggers a LiveQuery Event.created() block
  4. In the Event.created() block, I check if the object is in the local datastore with a PFQuery()
  5. If the object is not in the local datastore, then it is .pinned()
  6. If it is in the local datastore, the object is ignored.

Running with this logic, the new UserRelationship is showing within the Event.created() block that it is not in the local datastore, and attempts to .pin() it. That is when the following error occurs:

2020-06-03 14:44:06.650707-0500 SwagLeagues[1378:21947] [Error]: Attempted to change an objectId to one that's already known to the OfflineStore. className: UserRelationship old: (null), new: BjFwzHD4s7

I triple and quadruple checked, and I am in no way calling a .pin() on the object except in the Event.created() block. The only logical explanation I can think of is that Parse is adding a sort of "placeholder" object, with no objectId, in the local datastore when I create the new UserRelationship object. That "placeholder" object is then conflicting with my attempt to .pin() the object post save to the local datastore. This is VERY confusing...

EDIT 2: After doing a bunch more testing.... I'm starting to draw a conclusion I don't like. There are some instances where the new object is pinned successfully without issue, and others where the error occurs. It APPEARS that the 'UserRelationship' objects that are causing this error are ones where the relationship previously existed in the offline storage, and the "ghost" of their existence is causing this error.

Let me explain: In order to "flush" the offline storage for testing, I have been deleting the app off the device/simulator, assuming this would completely clear any and all data tables stored by the app. I would then rebuild the app on the device/simulator, assuming I had fresh and clear offline storage database tables.

This appears to not be the case. Does deleting the app not completely destroy any offline storage database tables created by the app?

Edit 3: I found the option in the simulator to "Erase All Content And Settings". I performed this on the simulator and then ran the app. The error is still occurring sporadically, but I now know the issue cannot be blamed on some sort of "ghost table" existing through deletion/installation.

0 Answers
Related