2

I am using some values to perform some calculations. For testing purposes I show in Label1 a value as string, since it is stored as a string and in Label2 I show a casted value as a Double since I need them at the end as doubles for my calculations.

The weird thing is, that when I access the ViewController the first time it doesn't show any values. But if I go back and klick on it again using the navigation controller it actually works. But I need the values right away cause my original intention is as I said, not showing some labels but rather making some calculations with it.

I made a little gif to show you what the problem is but I have problem with adding photos. Basically what happens is, that I click on the ViewController with the labels and nothing is showed. I go back and press again and the values will be showed in the labels.

Why is that and how can it be showed right away/ used for calculations right away

Thanks for the help. :)

class AHPfinalPreferencesViewController: UIViewController {

  var ahpPrios = [AHPPriorityStruct]()
    let decoder = JSONDecoder()

    @IBOutlet weak var label1: UILabel!
    @IBOutlet weak var label2: UILabel!
    
   
    let ajkpXc = globaLajkpXc
    let ajkpXijr = globaLajkpXijr

    let valueA = globaLajkpXc
    let valueB = Double(globaLajkpXijr)


 override func viewWillAppear(_ animated: Bool) {
         super.viewWillAppear(animated)
        UserService.ahpPref(for: User.current) { (ahpPrios) in
                self.ahpPrios = ahpPrios
                print("This is our AHP PRIOS", ahpPrios)
                

                for ahpPrio in ahpPrios {
                  print(ahpPrio)
                }
              
                print("this is the global ajk. ", self.ajkpXc)
        }
         
            
     }
        
    
    override func viewDidLoad() {
        super.viewDidLoad()

// Mark: - Get Data
      
             label1.text = valueA
             label2.text = "\(String(describing: valueB))"

        
// MARK: - Set Values for calculation
        
//        setValues()
//        ahpCalculation()
        
    }
  }

Could it be because of the globalVariables? I know that it is not the right way to do it but for my purposes its absolutely "okay"

import Foundation
import FirebaseAuth.FIRUser
import FirebaseDatabase
import FirebaseUI
import FirebaseAuth
import CodableFirebase

var globaLajkpXc: String = String()
var globaLajkpXijr: String = String()
var globaLajkpXqpa: String = String()


struct UserService {

static func ahpPref(for user: User, completion: @escaping ([AHPPriorityStruct]) -> Void) {
       
        
        let ref = Database.database().reference().child("AHPRatings").child(user.uid)
        
        
                ref.observe(DataEventType.value, with: { snapshot in
                    guard let value = snapshot.value else { return }
                    do {
                        let ahpPrios = try FirebaseDecoder().decode(AHPPriorityStruct.self, from: value)
                        print(ahpPrios)
                        
                        
                        // MARK: - lets store the values in the actual constants :)
                        
                        let ajkpXc = ahpPrios.ajkpXc
                        let ajkpXijr = ahpPrios.ajkpXijr
                        let ajkpXqpa = ahpPrios.ajkpXqpa

                             globaLajkpXc = ajkpXc ?? "no Value"
                             globaLajkpXijr = ajkpXijr ?? "no Value"
                             globaLajkpXqpa = ajkpXqpa ?? "no Value"

} catch let error {
                        print(error)
                    }
                })
        
     
        
          }

  }  


  [1]: https://i.stack.imgur.com/VKxaE.png
Schaedel420
  • 175
  • 11
  • I was trying to do exactly what you were saying. ```` override func viewDidLoad() { super.viewDidLoad() UserService.ahpPref(for: User.current) { (ahpPrios) in self.ahpPrios = ahpPrios print("This is our AHP PRIOS", ahpPrios) } self.label1.text = self.valueA self.label2.text = "\(String(describing: self.valueB))" } ```` – Schaedel420 Jul 14 '20 at 18:21

1 Answers1

1

You are calling UserService's ahpPref in your controller's viewWillAppear. BUT you are attempting to put your valueA (globaLajkpXc's value) to your label in your controller's viewDidLoad.

So what does that mean? Do you know which of these two controller's life cycle method gets called and when they do get called?

To solve your problem, have your label assigning value code

label1.text = globaLajkpXc

move in the completion block of your ahpPref (in the viewWillAppear).

Here's the Apple's documentation about the UIViewController's lifecycle: https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/WorkWithViewControllers.html


Also, below this line: globaLajkpXqpa = ajkpXqpa ?? "no Value"

add your completion call, like:

completion([ahpPrios]).

This should make my answer above work.

Glenn Posadas
  • 12,555
  • 6
  • 54
  • 95
  • I did what you said and it didn – Schaedel420 Jul 14 '20 at 18:24
  • 1
    See my edit. Add a completion call in your service. – Glenn Posadas Jul 15 '20 at 00:38
  • Thanks for your answer and sorry for that weird comment. I thought I deleted it and wanted to write a proper one this morning ;). I'll tried what you said but I still only get the values when I reload the Storyboard. Any other hint why this could be the problem? If not Im gonna accept your answer of course :) – Schaedel420 Jul 15 '20 at 06:42
  • 1
    Sure. I don't think there's other way around to fix your problem. I edited my answer above once more. I made a mistake, the `valueA` is a constant and it gets a value just once... To reiterate the solution and the issue I found in your question, first off, do the `label.text = globaLajkpXc ` in your viewWillAppear after the print statement inside the completion block. And then call the completion in your `static func ahpPref`. This now should really make it work. – Glenn Posadas Jul 15 '20 at 08:28
  • Thanks! It really worked! Thank you for keeping on that problem ;) Wish you a great day! – Schaedel420 Jul 15 '20 at 09:35