SwiftUI View not updating when #ObservedObject changes using GCD - ios

I'm still in the early stages with SwiftUI. Everything's been fine up until now, but I'm running into problems with using GCD - never worked with multi-threading before.
I am getting data from various sensors over TCP continuously running in the background. All this seems fine, I can see the data coming across and it appears to be getting stored where I want it...
Raw data
$AIMTW,26.1,C*1E
Contents of struct
(src: "AI", timeStamp: 2021-09-11 10:43:01 +0000, temp: 26.1, units: "ºC")
I think I'm getting the data processing happening on the main thread where it should update my View, but it doesn't update. I expect I've missed something pretty fundamental, but I just can't work out what it is. Any help would be appreciated.
This should hopefully be enough to reproduce the problem. Originally the code would be reading in a constant stream of data from a TCP source; I have modified it to read from a text file. The View should update after each line of the text file.
import Foundation
struct NMEA{
// Mean Temperature of Water
var mtw = (src: "", // Source
timeStamp: Date(), // Generated Time Stamp
temp: 0.0, // temperature
units: "") // unit of measurement, Celsius (C)
}
import SwiftUI
struct ContentView: View {
#ObservedObject var nmeaHandler = NMEAhandler()
var body: some View {
HStack{
Text("Water Temperature: ")
Text(String(nmeaHandler.nmea.mtw.temp))
Text(nmeaHandler.nmea.mtw.units)
}
.onAppear{
nmeaHandler.fetchNMEA()
}
}
}
import Foundation
class NMEAhandler: ObservableObject{
#Published var nmea = NMEA()
func fetchNMEA() {
DispatchQueue.global(qos: .userInitiated).async { self.receiveNMEA()
}
}
private func receiveNMEA() {
do {
let path = Bundle.main.path(forResource: "reprex-input", ofType: "txt") ?? "" // file path for file "data.txt"
let message = try String(contentsOfFile: path, encoding: String.Encoding.utf8)
print(message)
message.enumerateLines(invoking: { (line, stop) -> () in
print("Line = \(line)")
})
DispatchQueue.main.async {
self.prepareSentences(data: message)
print("The View updates now.")
}
} catch {
print("Error:", error)
}
}
private func prepareSentences(data: String){
var messages: [String] = []
var fields: [String] = []
if data != "-"{
messages = data.components(separatedBy: "\r\n")
for message in messages{
fields = message.components(separatedBy: ",")
let lastField = fields.last
let splitLast = lastField?.components(separatedBy: "*") ?? [""]
fields.removeLast(1)
fields.append(contentsOf: splitLast)
let valid = checksum(message: message, checksum: fields.last ?? "")
if valid{
parseNMEA(fields: fields)
// The View should be updated by now
}
}
return
}
}
private func checksum(message: String, checksum: String) -> Bool {
let messageLength = message.count
let firstCharacter = message.first
if messageLength < 5 || firstCharacter != "$"{
return false
}
let actualMessage = message.slice(1, messageLength - 4)
var calcChecksum = 0
for c in actualMessage.utf8 {
calcChecksum = calcChecksum ^ Int(c)
}
if calcChecksum == Int(checksum, radix: 16) ?? 999{
return true
}
return false
}
private func parseNMEA(fields: [String]){
switch fields[0].slice(3, 5){
case "MTW":
nmea.mtw.src = fields[0].slice(1, 2)
nmea.mtw.timeStamp = Date()
nmea.mtw.temp = Double(fields[1]) ?? 0
nmea.mtw.units = "º" + fields[2]
print("MTW: \(nmea.mtw)")
default:
print("Missing sentence: \(fields[0].slice(1, 2))")
return
}
}
}
import Foundation
extension StringProtocol {
func slice(_ start: Int, _ end: Int) -> String {
if self.count >= end {
let lower = index(self.startIndex, offsetBy: start)
let upper = index(lower, offsetBy: end - start)
return String(self[lower...upper])
} else {
return self as! String
}
}
}
Text file used for input:
$AIMTW,24.7,C*1A
$AIMTW,24.9,C*14
$AIMTW,24.8,C*15
$SDMTW,19.4,C*08
$AIMTW,24.9,C*14
$SDMTW,19.4,C*08
$SDMTW,19.4,C*08
$AIMTW,24.7,C*1A
$SDMTW,19.6,C*0A
$AIMTW,23.9,C*13

Related

How to make sure that a string contains a whole word not just part of it

I need some code to make sure that if a whole word exists in a return formatted text file it is accepted and that, if only part of it is present, it is not considered.
If I type lau in the TextField it is accepted and I would rather the answer was false until a whole word is matched
Here is the file limited.txt I use in my project. Each word is on a separate line:
appetitive
appetitiveness
appetitost
appetize
appetized
appetizement
appetizer
appetizers
appetizing
appetizingly
appinite
appius
appl
applanate
applanation
applaud
applaudable
applaudably
applauded
applauder
applauders
applauding
applaudingly
applauds
applause
applauses
applausive
applausively
apple
appleberry
appleblossom
applecart
appled
appledrane
appledrone
applegrower
applejack
applejohn
applemonger
Thanks for your help
import SwiftUI
struct ContentView: View{
#ObservedObject var textFileStringContent: TexFileReader
#State private var text = ""
var body: some View{
VStack {
TextField("please type the word to check", text: $text)
// so that it does not matter if user capitalises a word
if textFileStringContent.data.contains(self.text.lowercased()) {
Text("part of it exists")
// I tried to code it in here but to no avail
// if it is a whole word {
// Text("congratulations it does exist")
// }
} else if !text.isEmpty {
Text("sorry no such word")
}
}.padding().font(.headline)
.navigationBarTitle("Word checker")
}
}
class TexFileReader: ObservableObject {
#Published var data: String = ""
init() { self.load(file: "limited") }
func load(file: String) {
if let filepath = Bundle.main.path(forResource: file, ofType: "txt") {
do {
let contents = try String(contentsOfFile: filepath)
DispatchQueue.main.async {
self.data = contents
print(self.data.contains("lau"))
// this prints true even if lau is not a whole word
// applaud
// applaudable
// applaudably
// applauded
// applauder
// applauders
// applauding
// applaudingly
// applauds
// applause
// applauses
// applausive
// applausively
// but present in each of these
// I need to make sure that the match is a whole word not just part of one
}
} catch let error as NSError {
print(error.localizedDescription)
}
} else {
print("File not found")
}
}
}
A possible way is to search with Regular Expression and the word boundary specifier \\b
if textFileStringContent.data.range(of: "\\b\(self.text)\\b", options: [.caseInsensitive, .regularExpression]) != nil {
You may check if it ends with a newline separator in your text file:
let textWithNewline = self.text.lowercased() + "\n"
if textFileStringContent.data.contains(textWithNewline) {
// it is a whole word
}
Foundation contains a language analysis engine NSLinguisticTagger which can do many things including finding specific words with locale sensitivity.
A simple implementation of what you're trying to do is:
//extension via https://stackoverflow.com/questions/15062458/shortcut-to-generate-an-nsrange-for-entire-length-of-nsstring/56391610#56391610
extension String {
func range(from nsRange: NSRange) -> Range<String.Index>? {
return Range(nsRange, in: self)
}
}
var tagger = NSLinguisticTagger(tagSchemes: [NSLinguisticTagScheme.tokenType], options: 0)
let baddata = """
applaud
applaudable
applaudably
applauded
applauder
applauders catlau
applauding
"""
let gooddata = """
applaud
applaudable
applaudably
applauded
applauder
applauders lau catlau
applauding
"""
var foundLau = false
tagger.string = baddata
tagger.enumerateTags(in: NSRange(location: 0, length: baddata.count), scheme: .tokenType, options: [.omitWhitespace]) { tag, tokenRange, _, _ in
if tag != nil, let range = baddata.range(from: tokenRange) {
let fragment = baddata[range]
if fragment.lowercased() == "lau" {
foundLau = true
}
}
}
print("found \"lau\" in baddata =", foundLau ? "true":"false")
tagger.string = gooddata
tagger.enumerateTags(in: NSRange(location: 0, length: gooddata.count), scheme: .tokenType, options: [.omitWhitespace]) { tag, tokenRange, _, _ in
if tag != nil, let range = gooddata.range(from: tokenRange) {
let fragment = gooddata[range]
if fragment.lowercased() == "lau" {
foundLau = true
}
}
}
print("found \"lau\" in gooddata =", foundLau ? "true":"false")
enumerateTags returns an NSRange which can be converted to Range for general Swift use.

Unwrapping optional value (returned by data.withUnsafeBytes(_:)) sometimes does not work with guard let

I have issue with guard let statement, which behaves strange. Whole code is below. Else block of statement guard let data = readData, let size = sizeOfData else ... in method readActivity(subdata: Data) is wrongly executed even thoug readData and sizeOfData are not nil.
Code
import Foundation
enum ActivityDataReaderError: Error {
case activityIsReadingOtherCentral
case bluetooth(Error?)
case staleData
}
protocol ActivityDataReaderDelegate: class {
func didReadActivity(data: Data)
func didFailToReadActivity(error: ActivityDataReaderError)
}
final class ActivityDataReader {
private var sizeOfData: Int?
private var isOtherDeviceReading: Bool {
// 0xFFFF
return sizeOfData == 65535
}
private var readData: Data?
var isEmpty: Bool {
return sizeOfData == nil
}
weak var delegate: ActivityDataReaderDelegate?
static func timestampValue(_ timestamp: UInt32) -> Data {
var value = timestamp
return Data(buffer: UnsafeBufferPointer(start: &value, count: 1))
}
func reset() {
readData = nil
sizeOfData = nil
NSLog("reset() -- \(Thread.current)")
}
func readActivity(data: Data?, error: Error? = nil) {
guard let data = data else {
delegate?.didFailToReadActivity(error: .bluetooth(error))
return
}
let isFirstChunk = readData == nil
if isFirstChunk {
let sizeData = data.subdata(in: 0..<2)
sizeOfData = sizeData.withUnsafeBytes { $0.pointee }
guard !isOtherDeviceReading else {
delegate?.didFailToReadActivity(error: .activityIsReadingOtherCentral)
return
}
NSLog(String("readActivity() Size of data: \(String(describing: sizeOfData))"))
let subdata = data.subdata(in: 2..<data.count)
readActivity(subdata: subdata)
} else {
readActivity(subdata: data)
}
}
private func readActivity(subdata: Data) {
if let lastReadData = readData {
readData = lastReadData + subdata
} else {
readData = subdata
}
guard let data = readData, let size = sizeOfData else {
NSLog("WTF? data:\(String(describing: readData)), "
+ "sizeOfData: \(String(describing: sizeOfData)), "
+ "thread: \(Thread.current)")
assertionFailure("WTF")
return
}
NSLog("subdata: \(String(describing: subdata)), "
+ "totalReadBytes: \(data.count), "
+ "size: \(size)")
if data.count == size {
delegate?.didReadActivity(data: data)
reset()
}
}
}
Test
Test which sometimes passes and sometimes crashes because of assertionFailure("WTF").
class ActivityDataServiceReaderTests: XCTestCase {
var service: ActivityDataReader?
override func setUp() {
super.setUp()
service = ActivityDataReader()
}
override func tearDown() {
service = nil
super.tearDown()
}
func testBufferIsNotEmpty() {
NSLog("testBufferIsNotEmpty thread: \(Thread.current)")
guard let service = service else { fatalError() }
let firstDataBytes = [UInt8.min]
let data1 = Data(bytes: [7, 0] + firstDataBytes)
service.readActivity(data: data1)
XCTAssertFalse(service.isEmpty)
service.reset()
XCTAssertTrue(service.isEmpty)
}
}
Log of console in case of crash
2018-10-25 14:53:30.033573+0200 GuardBug[84042:11188210] WTF? data:Optional(1 bytes), sizeOfData: Optional(7), thread: <NSThread: 0x600003399d00>{number = 1, name = main}
Environment
Xcode10
swift 4.1 with legacy build system
swift 4.2
In my opinion, there is no possible way to execute code in else block in guard let else block of method readActivity(subdata: Data). Everything is running on main thread. Am I misssing something? How is possible sometimes test passes and sometimes crasshes?
Thank you for any help.
Edit:
More narrow problem of guard let + data.withUnsafeBytes:
func testGuardLet() {
let data = Data(bytes: [7, 0, UInt8.min])
let sizeData = data.subdata(in: 0 ..< 2)
let size: Int? = sizeData.withUnsafeBytes { $0.pointee }
guard let unwrappedSize = size else {
NSLog("failure: \(size)")
XCTFail()
return
}
NSLog("success: \(unwrappedSize)")
}
Log:
2018-10-25 16:32:19.497540+0200 GuardBug[90576:11351167] failure: Optional(7)
Thanks to help at: https://forums.swift.org/t/unwrapping-value-with-guard-let-sometimes-does-not-work-with-result-from-data-withunsafebytes-0-pointee/17357 problem was with the line:
let size: Int? = sizeData.withUnsafeBytes { $0.pointee }
Where read data was downcasted to Optional Int (8 bytes long) but sizeData it self was just 2 bytes long. I have no idea how is possible it sometimes worked but solution -- which seems to work properly -- is to use method withUnsafeBytes in fallowing way:
let size = sizeData.withUnsafeBytes { (pointer: UnsafePointer<UInt16>) in pointer.pointee }
Returned value is not optional and has the proper type UInt16 (2 bytes long).
If you check Documentation, there is a warning:
Warning The byte pointer argument should not be stored and used
outside of the lifetime of the call to the closure.
Seems like You should deal with the size inside the closure body
func testGuardLet() {
let data = Data(bytes: [7, 0, UInt8.min])
var sizeData = data.subdata(in: 0 ..< 2)
withUnsafeBytes(of: &sizeData) { bytes in
print(bytes.count)
for byte in bytes {
print(byte)
}
}
let bytes = withUnsafeBytes(of: &sizeData) { bytes in
return bytes // BUGS ☠️☠️☠️
}
}

My weather data is not showing up in my view controller

I can't figure out why my view controller is not showing the data, even though I can see it in the output window.
Output:
Muḩāfaz̧at Al Jīzah
Clear
88.0
my code:
override func viewDidLoad() {
super.viewDidLoad()
loadCurrentWeather = currentWeatherData()
loadCurrentWeather.downloadWeatherData {
//setting uo UI to download data
self.updateTodayUI()
}
}
func updateTodayUI() {
locationLabel.text = loadCurrentWeather.cityName
weatherTypeLabel.text = loadCurrentWeather.weatherType
currentTempLabel.text = "\(loadCurrentWeather.currentTemp)"
weatherTypeImage.image = UIImage(named: loadCurrentWeather.weatherType)
}
My view controller in Xcode:
My view controller on iphone:
currentweatherData the code where I'm downloading the data form.
import UIKit
import Alamofire
class currentWeatherData {
var cityNameone: String!
var dateone: String!
var weatherTypeone: String!
var currentTempone: Double!
var cityName: String {
if cityNameone == nil {
cityNameone = ""
}
return cityNameone
}
var date: String {
if dateone == nil {
dateone = ""
}
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
dateFormatter.timeStyle = .none
let currentDate = dateFormatter.string(from: Date())
self.dateone = "Today, \(currentDate)"
return dateone
}
var weatherType: String{
if weatherTypeone == nil{
weatherTypeone = ""
}
return weatherTypeone
}
var currentTemp: Double {
if currentTempone == nil {
currentTempone = 0.0
}
return currentTempone
}
func downloadWeatherData(completed: DownloadComplete){
// to tell alamofire where to download the data
let weatherURL = URL (string: currentWeatherURL)!
Alamofire.request(weatherURL).responseJSON{ response in
let result = response.result
if let dictionary = result.value as? Dictionary<String, AnyObject>{
if let name = dictionary["name"] as? String {
self.cityNameone = name.capitalized
print(self.cityNameone ?? "No city name")
}
if let weather = dictionary["weather"] as? [Dictionary<String, AnyObject>]{
if let main = weather[0]["main"] as? String {
self.weatherTypeone = main.capitalized
print(self.weatherTypeone ?? "No weather type")
}
}
if let main = dictionary["main"] as? Dictionary<String, AnyObject> {
if let currentTemperature = main["temp"] as? Double {
let kelvintoFarenheit = (currentTemperature * (9/5) - 459.67)
let totalKelvinToFarenheit = Double(round(10 * kelvintoFarenheit/10))
self.currentTempone = totalKelvinToFarenheit
print(self.currentTempone ?? .nan)
}
}
}
}
completed()
}
}
Is problem with my code or my view controller? Is it something wrong with my constraints?
I can't seem to figure it out.
You are calling completed too early - before the JSON response arrives. You have to call it inside the closure of the responseJSON call instead:
Alamofire.request(weatherURL).responseJSON { response in
let result = response.result
// ...
completed()
}
I cannot see all of your code to troubleshoot, but you may have a concurrency issue. Try putting the call to updateTodayUI inside of viewDidLoad(_:) inside of an async block like this:
DispatchQueue.main.async {
updateTodayUI()
}
You can find more information on dispatch queues and concurrency in the documentation.

use of unresolved identifier error and csv error

I've been stuck on this for the past couple days. I've made progress on other things in my project but this is holding me back, now i'm at the point where I need to somehow overcome this. Anyways, I've posted a question before about this but I guess I need to be way more specific.
My issue is I need to be able to search through this .CSV file using the barcode and return all the information regarding it. I've done this successfully in c++ but don't know why i'm having so much difficulties in swift.
This is a part of the file (has about 150 in total):
TITLE,SKU,PRICE,Barcode,WEIGHT,BOX HEIGHT,BOX W_IDTH,BOX LENGTH
"No.27 Grapefruit Triple Scented Soy Candle, 7oz",EBWC-10027,72,6933083850573,4,14.8,29.1,20.4
"No.18 Green Tea Jasmine Triple Scented Soy Candle, 7oz",EBWC-10018,72,6933083850580,4,14.8,29.1,20.4
Here is what i've done:
static func searchCode(codeNumber: String) -> [(title:String, sku:String, price:String, barcodeNum:String, weight:String, box_height:String, box_width:String, box_length:String )]?
{
//let test = "6933083850771"
let characterNumber = codeNumber.characters.count
//var dataArray = [Data]()
let barcodeNumber = codeNumber
if let remoteURL = NSURL(string: downloadconstants.URLConstants.productfile ){
while let line: String = barcodeNumber{
// Load the CSV file and parse it
let delimiter = ","
var items:[(title:String, sku:String, price:String, barcodeNum:String, weight:String, box_height:String, box_width:String, box_length:String )]?
do {
let content = try String(codeNumber)
print(content)
items = []
let lines:[String] = content.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet()) as [String]
for line in lines {
var values:[String] = []
if line != "" {
// For a line with double quotes
// we use NSScanner to perform the parsing
if line.rangeOfString("\"") != nil {
var textToScan:String = line
var value:NSString?
var textScanner:NSScanner = NSScanner(string: textToScan)
while textScanner.string != "" {
if (textScanner.string as NSString).substringToIndex(1) == "\"" {
textScanner.scanLocation += 1
textScanner.scanUpToString("\"", intoString: &value)
textScanner.scanLocation += 1
} else {
textScanner.scanUpToString(delimiter, intoString: &value)
}
// Store the value into the values array
values.append(value as! String)
// Retrieve the unscanned remainder of the string
if textScanner.scanLocation < textScanner.string.characters.count {
textToScan = (textScanner.string as NSString).substringFromIndex(textScanner.scanLocation + 1)
} else {
textToScan = ""
}
textScanner = NSScanner(string: textToScan)
}
// For a line without double quotes, we can simply separate the string
// by using the delimiter (e.g. comma)
} else {
values = line.componentsSeparatedByString(delimiter)
}
// Put the values into the tuple and add it to the items array
let item = (title: values[0], sku: values[1], price: values[2], barcodeNum: values[3], weight: values[4], box_height: values[5], box_width: values[6], box_length: values[7])
items?.append(item)
}
}
} catch {
print(error)
}
}
}
//Error: Use of unresolved identifier "items"
return items
}
I'm getting an error on the return. I've read online a bit, but no luck. Also for what I need my function to do what should be changed to make it work?

How to display detected barcode information

I'm creating an iphone app using swift 2. I got the app to be able to read barcodes and display the barcode. Also I learned core data and using it to import a .csv file with a bunch of product information which includes the barcode. Now when a barcode is scanned, I want it to search through the file and display the information relating to it. Here are parts of my code, how will I go about doing this? To be honest, I have programmers block right now and had for the past two days. Any help or guidance will be appreciated.
Here is the CSV Parser:
func parseCSV (contentsOfURL: NSURL, encoding: NSStringEncoding) -> [(title:String, price:String, weight:String, box_length:String, box_width:String, box_height:String, sku:String, barcodeNum:String )]? {
// Load the CSV file and parse it
let delimiter = ","
var items:[(title:String, price:String, weight:String, box_length:String, box_width:String, box_height:String, sku:String, barcodeNum:String )]?
do {
let content = try String(contentsOfURL: contentsOfURL, encoding: encoding)
print(content)
items = []
let lines:[String] = content.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet()) as [String]
for line in lines {
var values:[String] = []
if line != "" {
// For a line with double quotes
// we use NSScanner to perform the parsing
if line.rangeOfString("\"") != nil {
var textToScan:String = line
var value:NSString?
var textScanner:NSScanner = NSScanner(string: textToScan)
while textScanner.string != "" {
if (textScanner.string as NSString).substringToIndex(1) == "\"" {
textScanner.scanLocation += 1
textScanner.scanUpToString("\"", intoString: &value)
textScanner.scanLocation += 1
} else {
textScanner.scanUpToString(delimiter, intoString: &value)
}
// Store the value into the values array
values.append(value as! String)
// Retrieve the unscanned remainder of the string
if textScanner.scanLocation < textScanner.string.characters.count {
textToScan = (textScanner.string as NSString).substringFromIndex(textScanner.scanLocation + 1)
} else {
textToScan = ""
}
textScanner = NSScanner(string: textToScan)
}
// For a line without double quotes, we can simply separate the string
// by using the delimiter (e.g. comma)
} else {
values = line.componentsSeparatedByString(delimiter)
}
// Put the values into the tuple and add it to the items array
//(title:String, price:String, weight:String, box_length:String, box_width:String, box_height:String, sku:String, barcodeNum:String )
let item = (title: values[0], sku: values[1], price: values[2], weight: values[3], box_length: values[4], box_width: values[5], box_height: values[6], barcodeNum: values[7])
items?.append(item)
}
}
} catch {
print(error)
}
return items
}
When Barcode is detected:
func barcodeDetected(code: String) {
print(code)
if(barcodeDelegate != nil){
barcodeDelegate!.barcodeFound(code)
}
// Let the user know we've found something.
let alert = UIAlertController(title: "Found a Barcode!", message: code, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Search", style: UIAlertActionStyle.Destructive, handler: { action in
// Remove the spaces.
let trimmedCode = code.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
// EAN or UPC?
// Check for added "0" at beginning of code.
let trimmedCodeString = "\(trimmedCode)"
var trimmedCodeNoZero: String
if trimmedCodeString.hasPrefix("0") && trimmedCodeString.characters.count > 1 {
trimmedCodeNoZero = String(trimmedCodeString.characters.dropFirst())
// Send the doctored UPC to DataService.searchAPI()
DataService.searchCode(trimmedCodeNoZero)
} else {
// Send the doctored EAN to DataService.searchAPI()
DataService.searchCode(trimmedCodeString)
}
self.navigationController?.popViewControllerAnimated(true)
}))
self.presentViewController(alert, animated: true, completion: nil)
}
Dataservice.swift file
import Foundation
class DataService {
static let dataService = DataService()
static func searchCode(codeNumber: String) {
}
}
I can post more of my project if it helps, but I feel like this is already too much information, anything will help at this point on how to tackle this.

Resources