Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
Swift/IOS cómo actualizar usando observadores

En Android, tenemos un objeto de datos en vivo mutable cuando lo actualizamos, digamos en un hilo diferente que le adjuntamos a un observador. Cuando el objeto se actualiza, el observador está escuchando y actualiza la interfaz de usuario en consecuencia.

 liveDataService.setValue(response); ==> updating the object with new data

Y en la Actividad (digamos como TableViewController para swift/IOS)

Ponemos al observador

 liveDataService.observeForever(s -> { Log.d(TAG, getString(R.string.service_observer_message)); updateUI(s); }); ==> When liveDataService changes we update the UI

Ahora quiero hacer lo mismo para Swift/IOS

veo la funcion

 addObserver(_:forKeyPath:options:context:)

Pero no puedo adjuntarlo a un Array que recibe la actualización en segundo plano solo para NSObject

¿Cuál es el mejor método para realizar esta tarea?

gracias eran

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Según tengo entendido, debe usar notificaciones locales para enviar datos con addObserver y postNotification para lograr esto. encuentre el código de muestra para el mismo.

 let imageData:[String: UIImage] = ["image": image]

// publicar una notificación

 NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: imageData)

// Regístrese para recibir notificaciones en su clase

 NotificationCenter.default.addObserver(self, selector: #selector(self.receiveData(_:)), name: NSNotification.Name(rawValue: "notificationName"), object: nil)

// manejar notificación

 func receiveData(_ notification: NSNotification) { if let image = notification.userInfo?["image"] as? UIImage { } }
over 4 years ago · Santiago Trujillo Report

0

Puede usar la propiedad didSet observe. Responde cuando cambia el valor de la propiedad.

 var dataArray = [String]() { didSet { print(“Array Count = \(dataArray.count)”) } }

cuando agrega datos en dataArray, llama a la propiedad dataArray, observe e imprima el Array Count

over 4 years ago · Santiago Trujillo Report

0

Hay varias formas de hacerlo.

El que más se acerque a su solución de Android debería ser Key Value Observing: https://developer.apple.com/documentation/swift/cocoa_design_patterns/using_key-value_observing_in_swift

Si desea leer más sobre ese tema: https://www.swiftbysundell.com/posts/observers-in-swift-part-1

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!