[Swift] @StateObjectと@ObservedObject

@StateObjectを利用することで親ビューの再描画でオブジェクトが初期化されなくなる

ContentView.swift

struct ContentView: View {
    @State var isShow = true

    var body: some View {
        VStack (alignment: .leading, spacing: 20){
            ValueView1()
            ValueView2()
            
            Toggle(isOn: $isShow){
                
            }.frame(width: 50).padding(.top, 30)
            if isShow {
                Text("Hello, World!").font(.largeTitle)
            }
            Spacer()
        }
        .padding()
    }
}

struct ValueView2: View {
    @StateObject var maker = ValueMaker()
    
    var body: some View {
        VStack (alignment: .leading, spacing: 10){
            Text("\(maker.value)")
                .font(.title)
                .foregroundColor((maker.value > 0.8) ? .white : .gray)
                .background((maker.value > 0.8) ? Color.red : Color.white)
            HStack {
                Text("カウンタ:")
                Text("\(maker.counter)").font(.largeTitle)
            }
        }
        .background(Color.blue.opacity(0.3))
        .frame(width: 200, height: 80)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
        ValueView1()
        ValueView2()
    }
}

ValueMarker.swift

class ValueMaker: ObservableObject {
    
    @Published var value: Double
    @Published var counter: Int = 0
    private var timer: Timer
    
    init() {
        value = 0.0
        timer = Timer()
        start()
    }
    
    func start(){
        timer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) {
            _ in
            self.value = Double.random(in: 0 ... 1)
            if self.value > 0.8 {
                self.counter += 1
            }
        }
    }
}

struct ValueView1: View {
    @ObservedObject var maker = ValueMaker()
    
    var body: some View {
        VStack (alignment: .leading, spacing: 10){
            Text("\(maker.value)")
                .font(.title)
                .foregroundColor((maker.value > 0.8) ? .white : .gray)
                .background((maker.value > 0.8) ? Color.red : Color.white)
            HStack {
                Text("カウンタ:")
                Text("\(maker.counter)").font(.largeTitle)
            }
        }
        .background(Color.yellow.opacity(0.3))
        .frame(width: 200, height: 80)
    }
}

なんかうまくいかんなー

CoreDataのrelational

MySQLのconcatに近いか。
userFetch.predicate = NSPredicate(format: “name = %@”, “John”) で条件一致を求める。

override func viewDidLoad() {
        super.viewDidLoad()
        
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let managedContext = appDelegate.persistentContainer.viewContext
        let userEntity = NSEntityDescription.entity(forEntityName: "User", in: managedContext)!
        let user = NSManagedObject(entity: userEntity, insertInto: managedContext)
        
        user.setValue("John", forKeyPath: "name")
        user.setValue("john@test.com", forKey: "email")
        
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy/MM/dd"
        let date = formatter.date(from: "1990/10/08")
        user.setValue(date, forKey: "date_of_birth")
        
        user.setValue(0, forKey: "number_of_children")
        
        let carEntity = NSEntityDescription.entity(forEntityName: "Car", in:managedContext)!
        let car1 = NSManagedObject(entity: carEntity, insertInto: managedContext)
        car1.setValue("Audi TT", forKey: "model")
        car1.setValue(2010, forKey: "year")
        car1.setValue(user, forKey: "user")
        
        let car2 = NSManagedObject(entity: carEntity, insertInto: managedContext)
        car2.setValue("BMW X6", forKey: "model")
        car2.setValue(2014, forKey:"year")
        car2.setValue(user, forKey: "user")
        
        do {
            try managedContext.save()
        } catch {
            print("Failed saving")
        }

        let userFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "User")
        userFetch.fetchLimit = 1
        userFetch.predicate = NSPredicate(format: "name = %@", "John")
        userFetch.sortDescriptors = [NSSortDescriptor.init(key: "email", ascending: true)]
        
        let users = try! managedContext.fetch(userFetch)
        
        let john: User = users.first as! User
        
        print("Email: \(john.email!)")
        let johnCars = john.cars?.allObjects as! [Car]
        print("has \(johnCars.count)")

    }

ちょっと雑だが、次は地図に行ってみよう。

fetch results from core data

override func viewDidLoad() {
        super.viewDidLoad()
        
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let context = appDelegate.persistentContainer.viewContext
        let entity = NSEntityDescription.entity(forEntityName: "Users", in: context)
        let newUser = NSManagedObject(entity: entity!, insertInto: context)
        
        newUser.setValue("Louis Miguel", forKey: "username")
        newUser.setValue("asdf", forKey: "password")
        newUser.setValue("1", forKey: "age")
        
        do {
            try context.save()
        } catch {
            print("Failed saving")
        }
        
        let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Users")
        
        // request.predicate = NSPredicate(format: "age = %@", "12")
        request.returnsObjectsAsFaults = false
        
        do {
            let result = try context.fetch(request)
            for data in result as! [NSManagedObject] {
            print(data.value(forKey: "username"))
        }
    } catch {
        print("Failed")
    }
    }

「View」メニューの「Debug Area」から「Activate Console」でconsoleを表示します。
consoleにfetchしたデータが表示されています。
optionalと出ていますね。

オプショナル型とは変数にnilの代入を許容するデータ型で、反対に非オプショナル型はnilを代入できない。オプショナル型の変数にはデータ型の最後に「?」か「!」をつける。

usernameの箇所をprint(data.value(forKey: “password”))と変更します。
insertしたpasswordがfetchされ、表示されます。
しかし、何故printが繰り返されるのか?