Skip to content

feat: make "current" ParseObjects immutable #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

### main

[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/2.0.3...main)
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/2.1.0...main)
* _Contributing to this repo? Add info about your change here to be included in the next release_

### 2.1.0
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/2.0.3...2.1.0)

__Improvements__
- Make ParseUser.current, ParseInstallation.current, ParseConfig.current immutable. This prevents accidently setting to nil. When developers want to make changes, they should make mutable copies, mutate, then save ([#266](https://github.com/parse-community/Parse-Swift/pull/266)), thanks to [Corey Baker](https://github.com/cbaker6).

### 2.0.3
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/2.0.2...2.0.3)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ do {

//: Create your own value typed `ParseObject`.
struct GameScore: ParseObject {
//: Those are required for Object
//: These are required by ParseObject
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
Expand Down Expand Up @@ -69,7 +69,7 @@ extension GameScore {
}

struct GameData: ParseObject {
//: Those are required for Object
//: These are required by ParseObject
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct Cloud: ParseCloud {
//: Return type of your Cloud Function
typealias ReturnType = String

//: These are required for Object
//: These are required by `ParseCloud`
var functionJobName: String

//: If your cloud function takes arguments, they can be passed by creating properties:
Expand Down Expand Up @@ -86,7 +86,7 @@ cloudError.runFunction { result in
//: Saving objects with context for beforeSave, afterSave, etc.
//: Create your own value typed `ParseObject`.
struct GameScore: ParseObject {
//: Those are required for Object
//: These are required by ParseObject
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ initializeParse()

//: Create your own value typed ParseObject.
struct GameScore: ParseObject {
//: These are required for any Object.
//: These are required by `ParseObject`.
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ PlaygroundPage.current.needsIndefiniteExecution = true
initializeParse()

struct User: ParseUser {
//: These are required for `ParseObject`.
//: These are required by `ParseObject`.
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?

//: These are required for `ParseUser`.
//: These are required by `ParseUser`.
var username: String?
var email: String?
var emailVerified: Bool?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ PlaygroundPage.current.needsIndefiniteExecution = true
initializeParse()

struct GameScore: ParseObject {
//: Those are required for Object.
//: These are required by ParseObject.
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ initializeParseCustomObjectId()

//: Create your own value typed `ParseObject`.
struct GameScore: ParseObject {
//: Those are required for Object
//: These are required by ParseObject
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ initializeParse()
//: Create your own value typed ParseObject.
struct GameScore: ParseObject {

//: These are required for any Object.
//: These are required by `ParseObject`.
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ initializeParse()
//: Create your own value typed ParseObject.
struct GameScore: ParseObject {

//: These are required for any Object.
//: These are required by `ParseObject`.
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ initializeParse()

//: Create your own value typed ParseObject.
struct GameScore: ParseObject {
//: These are required for any Object.
//: These are required by `ParseObject`.
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import ParseSwift
initializeParse()

struct User: ParseUser {
//: These are required for `ParseObject`.
//: These are required by `ParseObject`.
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?

//: These are required for `ParseUser`.
//: These are required by `ParseUser`.
var username: String?
var email: String?
var emailVerified: Bool?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ PlaygroundPage.current.needsIndefiniteExecution = true
initializeParse()

struct User: ParseUser {
//: These are required for `ParseObject`.
//: These are required by `ParseObject`.
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?

//: These are required for `ParseUser`.
//: These are required by `ParseUser`.
var username: String?
var email: String?
var emailVerified: Bool?
Expand Down Expand Up @@ -61,7 +61,7 @@ extension User {

//: Create your own value typed `ParseObject`.
struct GameScore: ParseObject {
//: Those are required for Object
//: These are required by ParseObject
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ do {

//: Create your own value typed ParseObject.
struct GameScore: ParseObject {
//: Those are required for Object
//: These are required by ParseObject
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ PlaygroundPage.current.needsIndefiniteExecution = true
initializeParse()

struct Installation: ParseInstallation {
//: These are required for `ParseObject`.
//: These are required by `ParseObject`.
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?

//: These are required for `ParseInstallation`.
//: These are required by `ParseInstallation`.
var installationId: String?
var deviceType: String?
var deviceToken: String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ initializeParse()

//: Create your own value typed `ParseObject`.
struct GameScore: ParseObject {
//: Those are required for Object.
//: These are required by ParseObject.
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ initializeParse()

//: Create your own value typed `ParseObject`.
struct Book: ParseObject {
//: Those are required for Object
//: These are required by ParseObject
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
Expand All @@ -36,7 +36,7 @@ extension Book {
}

struct Author: ParseObject {
//: Those are required for Object.
//: These are required by ParseObject.
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ initializeParse()

//: Create your own value typed `ParseObject`.
struct GameScore: ParseObject {
//: Those are required for Object
//: These are required by ParseObject
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
Expand Down
6 changes: 3 additions & 3 deletions Sources/ParseSwift/Objects/ParseInstallation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ struct CurrentInstallationContainer<T: ParseInstallation>: Codable {
}

// MARK: Current Installation Support
extension ParseInstallation {
static var currentContainer: CurrentInstallationContainer<Self> {
public extension ParseInstallation {
internal static var currentContainer: CurrentInstallationContainer<Self> {
get {
guard let installationInMemory: CurrentInstallationContainer<Self> =
try? ParseStorage.shared.get(valueFor: ParseStorage.Keys.currentInstallation) else {
Expand Down Expand Up @@ -210,7 +210,7 @@ extension ParseInstallation {

- returns: Returns a `ParseInstallation` that is the current device. If there is none, returns `nil`.
*/
public static var current: Self? {
internal(set) static var current: Self? {
get {
return Self.currentContainer.currentInstallation
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/ParseSwift/Objects/ParseUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ struct CurrentUserContainer<T: ParseUser>: Codable {
}

// MARK: Current User Support
extension ParseUser {
static var currentContainer: CurrentUserContainer<Self>? {
public extension ParseUser {
internal static var currentContainer: CurrentUserContainer<Self>? {
get {
guard let currentUserInMemory: CurrentUserContainer<Self>
= try? ParseStorage.shared.get(valueFor: ParseStorage.Keys.currentUser) else {
Expand Down Expand Up @@ -137,7 +137,7 @@ extension ParseUser {
- returns: Returns a `ParseUser` that is the currently logged in user. If there is none, returns `nil`.
- warning: Only use `current` users on the main thread as as modifications to `current` have to be unique.
*/
public static var current: Self? {
internal(set) static var current: Self? {
get { Self.currentContainer?.currentUser }
set {
Self.currentContainer?.currentUser = newValue
Expand All @@ -149,7 +149,7 @@ extension ParseUser {

This is set by the server upon successful authentication.
*/
public var sessionToken: String? {
var sessionToken: String? {
Self.currentContainer?.sessionToken
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/ParseSwift/ParseConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation

enum ParseConstants {
static let sdk = "swift"
static let version = "2.0.3"
static let version = "2.1.0"
static let fileManagementDirectory = "parse/"
static let fileManagementPrivateDocumentsDirectory = "Private Documents/"
static let fileManagementLibraryDirectory = "Library/"
Expand Down
16 changes: 8 additions & 8 deletions Sources/ParseSwift/Types/ParseACL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -311,20 +311,20 @@ extension ParseACL {
aclController = try? ParseStorage.shared.get(valueFor: ParseStorage.Keys.defaultACL)
#endif

if aclController != nil {
if !aclController!.useCurrentUser {
return aclController!.defaultACL
if let acl = aclController {
if !acl.useCurrentUser {
return acl.defaultACL
} else {
guard let userObjectId = BaseParseUser.current?.objectId else {
return aclController!.defaultACL
return acl.defaultACL
}

guard let lastCurrentUserObjectId = aclController!.lastCurrentUserObjectId,
guard let lastCurrentUserObjectId = acl.lastCurrentUserObjectId,
userObjectId == lastCurrentUserObjectId else {
return try setDefaultACL(ParseACL(), withAccessForCurrentUser: true)
}

return aclController!.defaultACL
return acl.defaultACL
}
}

Expand Down Expand Up @@ -374,8 +374,8 @@ extension ParseACL {
}

let aclController: DefaultACL!
if modifiedACL != nil {
aclController = DefaultACL(defaultACL: modifiedACL!,
if let modified = modifiedACL {
aclController = DefaultACL(defaultACL: modified,
lastCurrentUserObjectId: currentUserObjectId,
useCurrentUser: withAccessForCurrentUser)
} else {
Expand Down
6 changes: 3 additions & 3 deletions Sources/ParseSwift/Types/ParseConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ struct CurrentConfigContainer<T: ParseConfig>: Codable {
var currentConfig: T?
}

extension ParseConfig {
public extension ParseConfig {

static var currentContainer: CurrentConfigContainer<Self>? {
internal static var currentContainer: CurrentConfigContainer<Self>? {
get {
guard let configInMemory: CurrentConfigContainer<Self> =
try? ParseStorage.shared.get(valueFor: ParseStorage.Keys.currentConfig) else {
Expand Down Expand Up @@ -165,7 +165,7 @@ extension ParseConfig {

- returns: Returns the latest `ParseConfig` on this device. If there is none, returns `nil`.
*/
public static var current: Self? {
internal(set) static var current: Self? {
get {
return Self.currentContainer?.currentConfig
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/ParseSwiftTests/APICommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ class APICommandTests: XCTestCase {

struct User: ParseUser {

//: Those are required for Object
//: These are required by ParseObject
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?

// provided by User
// These are required by ParseUser
var username: String?
var email: String?
var emailVerified: Bool?
Expand All @@ -73,7 +73,7 @@ class APICommandTests: XCTestCase {
var updatedAt: Date?
var ACL: ParseACL?

// provided by User
// These are required by ParseUser
var username: String?
var email: String?
var emailVerified: Bool?
Expand Down
2 changes: 1 addition & 1 deletion Tests/ParseSwiftTests/IOS13Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class IOS13Tests: XCTestCase { // swiftlint:disable:this type_body_length

struct GameScore: ParseObject {

//: Those are required for Object
//: These are required by ParseObject
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
Expand Down
Loading