     var userRecord: CKRecord? {
         didSet {
             if let userRecord = userRecord {
-                if let avatar = userRecord["avatar"] as? CKAsset {
-                    avatarImageView.image = UIImage(contentsOfFile: avatar.fileURL.path)
-                }
+                // User Record Found, extract user data to display
+                let profile = PerformerProfile() // start with blank profile
+                var cloudNeedsUpdating = false
+                // Avatar
+                if let avatarPath = userRecord[UserCloudKeys.avatar] as? CKAsset,
+                    let avatarImage = UIImage(contentsOfFile: avatarPath.fileURL.path) {
+                    profile.avatar = avatarImage
+                    print("USVC: Avatar found on Cloudkit.")
+                }
+                
+                // Stage Name
+                if let stageName = userRecord[UserCloudKeys.stagename] as? String {
+                    profile.stageName = stageName
+                    print("USVC: Stagename found on Cloudkit.")
+                } else if let stageName = UserDefaults.standard.string(forKey: SettingsKeys.performerKey) {
+                    profile.stageName = stageName
+                    print("USVC: Stagename found in UserDefaults (updating in cloud)")
+                    cloudNeedsUpdating = true
+                }
+                
+                // Jam Colour
+                if let jamHex = userRecord[UserCloudKeys.jamColour] as? String {
+                    profile.jamColour = UIColor(jamHex)
+                    print("USVC: jam colour found on Cloudkit.")
+                } else {
+                    let jamHue = UserDefaults.standard.float(forKey: SettingsKeys.performerColourKey)
+                    profile.jamColour = UIColor(hue: CGFloat(jamHue), saturation: 1.0, brightness: 0.7, alpha: 1.0)
+                    print("USVC: jam colour found in user defaults.")
+                    cloudNeedsUpdating = true
+                }
+                
+                // BG Colour
+                if let bgHex = userRecord[UserCloudKeys.backgroundColour] as? String {
+                    profile.backgroundColour = UIColor(bgHex)
+                    print("USVC: bg colour found on Cloudkit.")
+                } else {
+                    let bgHue = UserDefaults.standard.float(forKey: SettingsKeys.backgroundColourKey)
+                    profile.backgroundColour = UIColor(hue: CGFloat(bgHue), saturation: 1.0, brightness: 0.7, alpha: 1.0)
+                    print("USVC: bg colour found in UserDefaults.")
+                    cloudNeedsUpdating = true
+                }
+                
+                // SoundScheme
+                if let soundScheme = userRecord[UserCloudKeys.soundScheme] as? Int64 {
+                    profile.soundScheme = soundScheme
+                    print("USVC: soundscheme found on Cloudkit.")
+                } else {
+                    let soundScheme = UserDefaults.standard.integer(forKey: SettingsKeys.soundSchemeKey)
+                    profile.soundScheme = Int64(soundScheme)
+                    print("USVC: soundscheme found in userdefaults.")
+                    cloudNeedsUpdating = true
+                }
+                
+                if (cloudNeedsUpdating) {
+                    updateUserBasicProfile(userRecord, with: profile)
+                }
+                
+                updateUI(profile: profile)
+                userProfile = profile
                 avatarContainerView.isHidden = false
             } else {
+                // User Record not found, don't show user info fields.
                 avatarContainerView.isHidden = true
             }
         }
+    }
+    
+    /// Storage for a local copy of the users' profile data.
+    var userProfile: PerformerProfile?
+    
+    /// updates the profile screen's fields according to a PerformerProfile object
+    func updateUI(profile: PerformerProfile) {
+        avatarImageView.image = profile.avatar
+        stageNameField.text = profile.stageName
+        jamColourSlider.setValue(PerformerProfile.hueFrom(colour: profile.jamColour), animated: true)
+        jamColourSlider.tintColor = profile.jamColour
+        jamColourSlider.thumbTintColor = profile.jamColour
+        
+        backgroundColourSlider.tintColor = profile.backgroundColour
+        backgroundColourSlider.thumbTintColor = profile.backgroundColour
+        backgroundColourSlider.setValue(PerformerProfile.hueFrom(colour: profile.backgroundColour), animated: true)
     }
     
     

            
            updateUserRecord(userRecord, with: url, fallbackImage: previousImage)
        } catch {
            print("Error writing avatar to temporary directory: \(error)")
        }
    }
    
    private func updateUserRecord(_ userRecord: CKRecord, with avatarURL: URL, fallbackImage: UIImage?) {
        avatarSpinner.startAnimating()
        avatarImageView.alpha = 0.5
        
        userRecord["avatar"] = CKAsset(fileURL: avatarURL)
        
        container.publicCloudDatabase.save(userRecord) { _, error in
            defer {

    
    /// Triggered when user changes the jam colour slider
    @IBAction func jamSliderMoved(_ sender: UISlider) {
        let colour = PerformerProfile.colourFromHue(hue: sender.value)
        sender.tintColor = colour
        sender.thumbTintColor = colour
        if let profile = userProfile {
            profile.jamColour = colour
        }
    }
    
    /// Triggered when user changes the background colour
    @IBAction func backgroundSliderMoved(_ sender: UISlider) {
        let colour = PerformerProfile.colourFromHue(hue: sender.value)
        sender.tintColor = colour
        sender.thumbTintColor = colour
        if let profile = userProfile {
            profile.backgroundColour = colour
        }
    }
    
    /// Update the User's stageName in CloudKit
    private func updateUserStageName(_ userRecord: CKRecord, with stageName: String) {
        userRecord[UserCloudKeys.stagename] = stageName as CKRecordValue
        container.publicCloudDatabase.save(userRecord) { _, error in
            if (error != nil) {
                print("USVC: Error saving to cloudkit")
                print(error ?? "")
            } else {
                print("USVC: updated user in cloudkit", stageName)
            }
        }
    }
    
    /// Update basic profile info in CloudKit (stagename, colours, and soundscheme)
    /// Does not update avatar image.
    internal func updateUserBasicProfile(_ userRecord: CKRecord, with profile: PerformerProfile) {
        userRecord[UserCloudKeys.stagename] = profile.stageName as CKRecordValue
        userRecord[UserCloudKeys.jamColour] = profile.jamColour.hexString() as CKRecordValue
        userRecord[UserCloudKeys.backgroundColour] = profile.backgroundColour.hexString() as CKRecordValue
        userRecord[UserCloudKeys.soundScheme] = profile.soundScheme as CKRecordValue
        
        container.publicCloudDatabase.save(userRecord) { _, error in
            if (error != nil) {
                print("USVC: Error saving to cloudkit")
                print(error ?? "")
            } else {
                print("USVC: updated user profile in cloudkit.")
            }
        }
        
        // update user defaults
        // FIXME: use of userdefaults should be deprecated.
        UserDefaults.standard.set(profile.stageName, forKey: SettingsKeys.performerKey)
        UserDefaults.standard.set(PerformerProfile.hueFrom(colour: profile.jamColour), forKey: SettingsKeys.performerColourKey)
        UserDefaults.standard.set(PerformerProfile.hueFrom(colour: profile.backgroundColour), forKey: SettingsKeys.backgroundColourKey)
        UserDefaults.standard.set(profile.soundScheme, forKey: SettingsKeys.soundSchemeKey)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        print("USVC: view will disappear")
        if let profile = userProfile, let record = userRecord {
            updateUserBasicProfile(record, with: profile)
        }
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // When segue occurs, request to upload basic profile info to CloudKit
        print("USVC: preparing to segue")
    }
}

