Home > Mobile >  Data not saving correctly after upgrading Xcode 12.4 to 13.3.1
Data not saving correctly after upgrading Xcode 12.4 to 13.3.1

Time:05-22

I was working on an app using Xcode 12.4 and it was saving correctly. Now that I have got a new computer and upgraded to Xcode 13.3.1 when I enter "123456" it saves as "12345" if I enter "1234567" and then delete the "7" before saving it saves as "12345". If I enter "12345" or "1234567" and then hit save it works correctly.

I have no clue what part of the code could be causing this problem or what other information needs to be included.

I am thinking it is an Xcode problem and not a problem with my code.

I have a print statement on the save button and the code is wrong at that point. "123456" prints as "12345"

struct AddTripView: View {
    @Environment(\.presentationMode) var presentationMode
    @StateObject private var addTripViewModel: AddTripViewModel
    
    init(repo: RepositoryProtocol) {
        _addTripViewModel = StateObject<AddTripViewModel>.init(wrappedValue: AddTripViewModel(repo: repo))
    }
    
    var body: some View {
        Form {
            HStack {
                Text("BOL:").fontWeight(.bold)
                TextField("BOL", text: $addTripViewModel.bol).keyboardType(.numberPad)
            }
        }
        .toolbar {
            ToolbarItemGroup(placement: .navigationBarTrailing) {
                Button {
                    addTripViewModel.addTrip()
                    print("AddView \(addTripViewModel.bol)")
                    presentationMode.wrappedValue.dismiss()
                } label: {
                    Text("Save")
                }
            }
        }
    }
}
class AddTripViewModel: ObservableObject {
    private let repo: RepositoryProtocol
    @Published var saved: Bool = false
    
    var bol: String = ""
    
    init(repo: RepositoryProtocol) {
        self.repo = repo
    }
    
    func addTrip() {
        let trip = Trip(uid: "",
                        bol: bol)
        
        repo.addTrip(trip: trip) { result in
            switch result {
            case .success(let savedTrip):
                DispatchQueue.main.async {
                    self.saved = savedTrip == nil ? false : true
                }
            case .failure(let error):
                print(error.localizedDescription)
            }
        }
    }
}
class FirebaseRepository: RepositoryProtocol {
    private let db = Firestore.firestore()
    
    func getAllTrips(completion: @escaping (Result<[Trip]?, Error>) -> Void) {
        let userId = Auth.auth().currentUser?.uid
        
        db.collection("trips")
            .whereField("userId", isEqualTo: userId!)
            .getDocuments { snapshot, error in
                guard let snapshot = snapshot, error == nil else {
                    completion(.failure(error ?? NSError(domain: "snapshot is nil", code: 107, userInfo: nil)))
                    return
                }
                let trips: [Trip]? = snapshot.documents.compactMap { doc in
                    var trip = try? doc.data(as: Trip.self)
                    if trip != nil {
                        trip!.id = doc.documentID
                    }
                    return trip
                }
                completion(.success(trips))
            }
    }
    
    func addTrip(trip: Trip, completion: @escaping (Result<Trip?, Error>) -> Void) {
        do {
            var addedTrip = trip
            addedTrip.userId = Auth.auth().currentUser?.uid
            
            let reference = db.collection("trips").document()
            let uid = reference.documentID
            addedTrip.uid = uid
            try reference.setData(from: addedTrip)
        } catch {
            fatalError("Unable to encode trip: \(error.localizedDescription)")
        }
    }
}

CodePudding user response:

Change

var bol: String = ""

To

@Published var bol: String = ""
  • Related