Deleting / setting a pointer to nil

Hi, I need help trying to understand if I’m doing something wrong here. Whatever I’ve tried, I can’t seem to remove a pointer from my object and set it to nil.

  func removeProfilePhoto(for userProfile: UserProfile) async throws -> UserProfile? {
    var workingProfile = userProfile
    workingProfile.photo = nil
    let savedProfile = try await workingProfile.save()
    return savedProfile
  }

photo is a pointer to another object

Screen Shot 2021-11-06 at 1.19.33 AM

and every time I run this function, I check the dashboard but alas, the photo pointer is still there.

is there a special way to remove pointers from an object? I’ve tried googling but I nothing.

thanks in advance.

The reason why setting the value to nil doesn’t work is here:

Follow the procedure here to set a property to nil. Basically, you use ParseOperation to unset the property:

func removeProfilePhoto(for userProfile: UserProfile) async throws -> UserProfile? {
    let operations = userProfile.operation
       .unset(("photo", \.photo))
    return try await operations.save()
}
1 Like

ha.

The one place I forgot to check was the repo issues lol.

Thank you.

It is the docs too:

JS Guide

API Documentation:

This question was about the Swift SDK, the JS docs won’t help as they are for a different language.

So how do you go about setting a pointer field?

The playgrounds shows how to go about most of the basic functionality of the SDK:

Specifically:

Sorry I should have been more specific.

I have an object that contains a bool and a ParseFile like this:

struct ProfilePicture: ParseObject, Identifiable, Decodable
{
	// Mandatory properties
	var ACL: ParseACL?
	var createdAt: Date?
	var originalData: Data?
	var objectId: String?
	var updatedAt: Date?
	
	// Custom properties
	var userProfilePicture: ParseFile?
	var verified: Bool?
}

And I have a pointer in my user object like this:

struct User: ParseUser, ParseObject
{
	// Mandatory properties
	var ACL: ParseACL?
	var authData: [String: [String: String]?]?
	var createdAt: Date?
	var email: String?
	var emailVerified: Bool?
	var originalData: Data?
	var objectId: String?
	var password: String?
	var updatedAt: Date?
	var username: String?
	
	// Custom properties
	var firstName: String?
	var lastName: String?
	var matchInfo: MatchInfo?
	var profilePicture: ProfilePicture?
}

I learned from this post how to set the userProfilePicture field to nil using unset, but how to I go about assigning a new ProfilePicture object to the user field? I tried this code:

let imageData = unwrapImage.pngData()
let parseImageFile = ParseFile(data: imageData!)
var profilePicture = ProfilePicture()
profilePicture.verified = false
profilePicture.userProfilePicture = parseImageFile
profilePicture.save { _ in
	var currentUser = User.current
	currentUser?.profilePicture = profilePicture
	currentUser?.save { _ in
		User.current?.fetch(includeKeys: ["*"]) { result in
		}
	}
}

But it does not save the ProfilePicture object.

It looks like you missed saving the returned object from the Parse Server, so your code is probably saving multiple objects because you are referencing an unsaved profilePicture:

// Using completion blocks...
profilePicture.save { result in
  switch result {
  case .success(let updatedProfile):
    var currentUser = User.current
    currentUser?.profilePicture = updatedProfile
    currentUser?.save { _ in
      User.current?.fetch(includeKeys: ["*"]) { _ in }
    }
  case .failure(let error):
     // handle error
  }
}

// Using async/await may make it easier to see...

do {
  let updatedProfile = try await profilePicture.save()
  var currentUser = User.current
  currentUser?.profilePicture = updatedProfile
  let updatedUser = try await currentUser?.save()
  let fetchedUser = try await updatedUser?.fetch()
  ...
} catch {
  // handle error
}