[ad_1]
I’m constructing a climate app that makes use of core knowledge to retailer areas (‘areas’ in my app). Every little thing is working high-quality apart from one factor. Once I choose one in every of my areas (to vary the town being displayed on the house web page), core knowledge rearranges the order of my fetched outcomes. It would not occur each time, however more often than not it does and I am undecided why. Under are screenshots of my app and snippets of my code. I can present extra of my code if this isn’t sufficient to unravel or perceive the issue. Thanks upfront!
House Web page
import SwiftUI
struct House: View {
@State non-public var showingAddSpace = false
@State non-public var showingSpaces = false
@FetchRequest(sortDescriptors: []) var areas: FetchedResults<CachedSpace>
var physique: some View {
VStack {
HStack {
Button {
showingSpaces.toggle()
} label: {
Picture(systemName: "checklist.sprint")
.font(.title2)
}
Spacer()
Button {
showingAddSpace.toggle()
} label: {
Picture(systemName: "plus")
.font(.title2)
}
}
Textual content(areas.rely == 0 ? "San Jose" : areas[CachedSpace.activeIndex].wrappedCity)
.font(.title)
.fontWeight(.daring)
Spacer()
}
.padding()
.sheet(isPresented: $showingAddSpace) {
AddSpace()
}
.sheet(isPresented: $showingSpaces) {
Areas()
}
}
}
struct Home_Previews: PreviewProvider {
static var previews: some View {
House()
}
}
Areas Show Web page
import SwiftUI
struct Areas: View {
@Atmosphere(.managedObjectContext) var moc
@Atmosphere(.dismiss) var dismiss
@FetchRequest(sortDescriptors: []) var areas: FetchedResults<CachedSpace>
var physique: some View {
NavigationView {
Checklist {
ForEach(areas) { area in
HStack {
HStack {
Textual content(area.wrappedCity)
.fontWeight(.daring)
Textual content("(area.wrappedTemperature)°")
}
.onTapGesture {
if let index = areas.firstIndex(the place: {$0.id == area.id}) {
let index = index
CachedSpace.activeIndex = index
print(CachedSpace.activeIndex)
print(areas[CachedSpace.activeIndex].wrappedCity)
}
for s in areas {
s.lively = false
}
area.lively.toggle()
dismiss()
}
Spacer()
Button {
print(area)
} label: {
Picture(systemName: area.lively == true ? "star.fill" : "star")
}
}
}
.onDelete(carry out: deleteSpace)
}
.navigationTitle("Desmond's Areas")
}
}
func deleteSpace(at offsets: IndexSet) {
for offset in offsets {
let area = areas[offset]
moc.delete(area)
}
strive? moc.save()
}
}
struct Spaces_Previews: PreviewProvider {
static var previews: some View {
Areas()
}
}
Core Information Properties
import Basis
import CoreData
extension CachedSpace {
@nonobjc public class func fetchRequest() -> NSFetchRequest<CachedSpace> {
return NSFetchRequest<CachedSpace>(entityName: "CachedSpace")
}
@NSManaged public var id: UUID?
@NSManaged public var metropolis: String?
@NSManaged public var temperature: String?
@NSManaged public var lively: Bool
var wrappedCity: String {
metropolis ?? ""
}
var wrappedTemperature: String {
temperature ?? ""
}
static var activeIndex = 0
}
extension CachedSpace : Identifiable {
}
Web page to Add Areas
import SwiftUI
struct AddSpace: View {
@Atmosphere(.managedObjectContext) var moc
@Atmosphere(.dismiss) var dismiss
@FetchRequest(sortDescriptors: []) var areas: FetchedResults<CachedSpace>
@State non-public var metropolis = ""
@State non-public var temperature = ""
var physique: some View {
NavigationView {
Type {
TextField("Metropolis", textual content: $metropolis)
TextField("Temperature", textual content: $temperature)
Button("Save") {
let newSpace = CachedSpace(context: moc)
newSpace.metropolis = metropolis
newSpace.temperature = temperature
newSpace.lively = false
newSpace.id = UUID()
strive? moc.save()
dismiss()
}
}
}
}
}
struct AddSpace_Previews: PreviewProvider {
static var previews: some View {
AddSpace()
}
}
[ad_2]


