Post

Replies

Boosts

Views

Activity

CLLocationUpdate isStationary issue
Hi, Per Apple the isStationary value is supposed to set to true when the device is stationary. I am trying to get a better understanding of when and how this status is changed. When and how does Apple decide when to set this to true and what is the threshold by which it is set to false. Right now when I start my app and use it is set from true to false instantly. let updates = CLLocationUpdate.liveUpdates() for try await update in updates { self.isStationary = update.isStationary I would love to know by what criteria it sets it to true otherwise I'm collecting a lot of zero speed very slight to no movements in latitude and longitude that I have to make some assumptions about filtering out of what I capture. I can't seem to find any mention of this or use case examples in any of the usual sources for examples despite having been introduced at the last WWDC 2023. Any help here would be appreciated.
0
0
253
Apr ’24
Background location tracking challenge
I have a driving tracking app I'm working on that properly starts tracking and logging location when the app is in the foreground or background. I use a buffer/queue to keep recent locations so when a trip ramps up to driving speed I can record that to work back to the start location just before the trip starts. This works great, however, in background mode when the user does not have the app open it will record locations but not until a significant location change is detected. The buffering I do is lost and the location only starts tracking several hundred yards or more after the trip has started. Does anyone have any suggestions or strategies to handle this chicken and the egg scenario?
3
0
504
Apr ’24
Issues with NWPathMonitor
With SCNetworkReachabilityCreateWithAddress now depreciated I'm having to use NWPathMonitor to determine if I have a good connection to access some HTML and JSON files. I have this working just fine if the network connection is down but if the network connection comes back up it detects an event but still shows the status as unsatisfied. I can't seem to capture or pickup that when I've got a good connection again so I can proceed with the right status. Here is my current code. Any suggestions would be appreciated: import Network import Combine class NetworkStatus: ObservableObject { static let shared = NetworkStatus() private var monitor: NWPathMonitor? private var queue = DispatchQueue(label: "NetworkMonitor") @Published var isConnected: Bool = false private init() { monitor = NWPathMonitor() startMonitoring() } deinit { stopMonitoring() } func startMonitoring() { monitor?.pathUpdateHandler = { [weak self] path in DispatchQueue.main.async { let status = path.status == .satisfied self?.isConnected = status print(">>> \(path.status)") } } monitor?.start(queue: queue) } func stopMonitoring() { monitor?.cancel() monitor = nil } }
1
0
693
Mar ’24
CoreData/SwiftData persistent history error after adding additional models
I'm trying to add two more data models SectionsSD andArticles SD to an existing project using SwiftData where I had one model and am running into a variety of CoreData errors when I try to run my app. I am running Xcode 15.2 and the latest iOS simulator at iOS 17.2 Here is my container let container: ModelContainer = { let schema = Schema([ LocationData.self, SectionsSD.self, ArticlesSD.self ]) let container = try! ModelContainer(for: schema, configurations: []) return container }() After I add these two models I get the following errors and I can't load data into the SectionsSD and ArticlesSD models. If I comment out my LocataionData model that works w/o error or if I run just LocationData it runs without error but run all together and I get the following errors in the log: error: Error: Persistent History (11) has to be truncated due to the following entities being removed: ( SectionsSD, ArticlesSD ) CoreData: error: Error: Persistent History (11) has to be truncated due to the following entities being removed: ( SectionsSD, ArticlesSD ) warning: Warning: Dropping Indexes for Persistent History CoreData: warning: Warning: Dropping Indexes for Persistent History warning: Warning: Dropping Transactions prior to 11 for Persistent History CoreData: warning: Warning: Dropping Transactions prior to 11 for Persistent History warning: Warning: Dropping Changes prior to TransactionID 11 for Persistent History Any advice here would be appreciated. These are initial models so migration is not really a need yet. Seems to just **** if you add more models.
3
0
843
Feb ’24
Store location data when app is not running using Xcode 15.1 and SwiftUI
I'm close but can't seem to get my app to store location data when not running. I'm trying to collect driver data. When I start the App it asks for location permission while using the App. At no time does the app ask me to give permission for allowing the app to collect information when I am not using the app. I think the problem revolves around this. I've also tried going into iOS settings for the app and set to Always but that didn't help. I'm likely missing something here within the app to get it to collect the data when the app is not running. Any help is appreciated. Here is what I've got. For Signing I have Background Modes enabled with "Location updates". For plist.info I have the following set with descriptions. Privacy - Location Location Always Usage Description Privacy - Location When in Use Usage Description Privacy - LocationAways and When in Use Usage Description I also have in the Info file: Required background modes with Item 0 set with "App registers for location updates" for code I have the the following in the AppDelegate: import UIKit import CoreLocation class AppDelegate: NSObject, UIApplicationDelegate { static let shared = AppDelegate() func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool { // Add any custom setup code here return true } } extension AppDelegate { func requestLocationAuthorization() { let locationManager = CLLocationManager() locationManager.requestAlwaysAuthorization() } } For my app here is how I trigger the app delegate and starupt the location manager import SwiftUI import SwiftData @main struct ZenTracApp: App { @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate @Environment(\.scenePhase) var scenePhase @StateObject var locationManager = LocationManager() ... for code I have the following LocationManager: import CoreLocation import SwiftData @MainActor class LocationManager: NSObject, ObservableObject { @Published var location: CLLocation? //@Published var region = MKCoordinateRegion() private let locationManager = CLLocationManager() /// Override exiting init override init() { /// Bring in the normal init super.init() AppDelegate.shared.requestLocationAuthorization() locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.distanceFilter = kCLDistanceFilterNone locationManager.requestAlwaysAuthorization() locationManager.startUpdatingLocation() locationManager.delegate = self locationManager.allowsBackgroundLocationUpdates = true locationManager.showsBackgroundLocationIndicator = true locationManager.startUpdatingLocation() } } extension LocationManager: CLLocationManagerDelegate { /// iOS triggers whenever the location changes func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { guard let newLocation = locations.last else {return} /// Check if the location has changed significantly before updating in this case more than 10 meters if self.location == nil || location!.distance(from: newLocation) > 10 { self.location = newLocation // save the location info in this function saveEntry(location: self.location!) } } }
1
0
580
Dec ’23
Accessing a completion variable outside the function
I am retrieving a value from a remote json file through task in a function with a completion code (as recommended practice) where I retrieve a date. I then need to use that date in subsequent processing. In this case, return the date and continue my processing. I can work with the date with in the function (see below) but I can't get the value outside the function. Any help would be appreciated. case .remote: var fetchDate: Date? fetchRemoteArticlesDate { date in fetchDate = date // finds the correct date print("* fetchDate1: \(String(describing: fetchDate))" ) } // How can I receive the date from fetchRemoteArticlesData so I can return it? print("* fetchDate2: \(fetchDate ?? DateInfo.zeroDate)") return fetchDate ?? DateInfo.zeroDate class func fetchRemoteArticlesDate(completion: @escaping (Date) -> Void) { let urlString = AppValues.articlesLocation.remote let url = URL(string: urlString)! let session = URLSession.shared let task = session.dataTask(with: url) { (data, response, error) in if let error = error { LogEvent.print(module: "Articles.fetchArticles", message: "Error saving articles.json: \(error)") DispatchQueue.main.sync { completion(DateInfo.zeroDate) } return } guard let myData = data else { LogEvent.print(module: "Articles.fetchArticles", message: "no JSON data received") DispatchQueue.main.async { completion(DateInfo.zeroDate) } return } let date = decodeArticlesDateV2(myData) LogEvent.print(module: "Articles.articlesDate", message: "\(UserSettings.init().articlesLocation.description) date: \(date)") completion(date) } task.resume() }
2
0
315
Nov ’23
Can't remove GitHub repository
I give up and need some help. I picked up another GitHub repository into my project when adding a file from another project through Xcode add file/copy and so on. I can't seem to find out what file it's pointing to despite deleting it from the project. Also, I can't seem to get rid of the extra repository. Any help in how to clean this up would be appreciated. I want to get rid of the ex-login-1 GitHub in this project. That said I do not want to get rid of ex-login-1 as it's own GitHub Xcode project. I just need to get these two separated. On the ex-login-1 Xcode project it has no reference to the Zentastic project so this is one way.
4
0
799
Aug ’23
How to form a search predicate using swift data with a one to many model
I am working in Xcode 15 (beta) migrating to SwiftData and am having a hard time figuring out how to form a search predicate for my one to many model. The desired result is to return a query that returns only the articles and sections where the article "search" field contains the string a user is searching for. Here is my current model: @Model class SectionsSD { @Attribute(.unique) var id: String var section: String var rank: String var toArticles: [ArticlesSD]? init(id:String, section: String, rank: String) { self.id = id self.section = section self.rank = rank } } @Model class ArticlesSD { var id: String var title: String var summary: String var search: String var section: String var body: String @Relationship(inverse: \SectionsSD.toArticles) var toSection: SectionsSD? init(id: String, title: String, summary: String, search: String, section: String, body: String) { self.id = id self.title = title self.summary = summary self.search = search self.section = section self.body = body } } In CoreData I was able to do the following in my code to form and pass the search predicate ("filter" being the user input search text, "SectionsEntity" being my old CoreData model): _fetchRequest = FetchRequest<SectionsEntity>(sortDescriptors: [SortDescriptor(\.rank)], predicate: NSPredicate(format: "toArticles.search CONTAINS %@", filter)) I can't find any examples or info anywhere that explains how to form a predicate in SwiftData to achieve the same search results. I can't seem to find a way to represent toArticles.search properly assuming the capability is there. Here is what I've tried but Xcode complains about the $0.toArticles?.contains(filter) with errors about "Cannot convert value of type 'Bool?' to closure result type 'Bool'" and "Instance method 'contains' requires the types 'ArticlesSD' and 'String.Element' (aka 'Character') be equivalent" let searchPredicate = #Predicate<SectionsSD> { $0.toArticles?.contains(filter) } _sectionsSD = Query(filter: searchPredicate) I've tried $0.toArticles?.search.contains(filter) but Xcode can't seem to find its way there like it did using CoreData Any suggestions and examples on how to form a predicate in this use case would be appreciated.
7
1
4.5k
Jun ’23