Hoşgeldin. Soru sormak veya cevaplamak için hemen üye ol.
0 oy
920 kez görüntülendi
ios development kategorisinde tarafından
Merhaba Arkadaşlar,

Projenizde oluşturduğunuz database'yi içerisindeki verileri görüntülemek için nasıl bir yol izliyorsunuz.

1 cevap

0 oy
tarafından
tarafından seçilmiş
 
En İyi Cevap

core data'dan veri çekmek için executeFetchRequest'i kullanıyoruz.

şöyleki,

MyUsersManagedObject adlı bir NSManagedObject class'ımız olduğunu farz edersek,

import Foundation
import CoreData

    @objc(MyUsersManagedObject)
    class MyUsersManagedObject: NSManagedObject {

        @NSManaged var uniqueId: String
        @NSManaged var name: String
        @NSManaged var lastName: String
        @NSManaged var birthDate: NSDate



        override init(entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext?) {
            super.init(entity: entity, insertIntoManagedObjectContext: context)
        }

    }

bu modele göre "Users" adlı entity içerisinden yapacağım fetch request'im aşağıdaki gibi olmalıdır.

var users = [MyUsersManagedObject]()    

func fetchUsersFromCoreData() {
        var myManagedObjectContext:NSManagedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
        let fetchRequest = NSFetchRequest(entityName:"Users")
        fetchRequest.returnsObjectsAsFaults = false
        var error: NSError?
        let fetchedResults = myManagedObjectContext.executeFetchRequest(fetchRequest, error: &error) as! [MyUsersManagedObject]?
        if let results = fetchedResults {
            self. users.removeAll(keepCapacity: false)
            self. users = results
        } else {
            println("Could not fetch \(error), \(error!.userInfo)")
        }
    }
tarafından
Yasin hocam aslında demek istediğin daha önce android'te karşılaştıysanız örnek olarak emülatör içerisinde oluşturulan databaseyi görmek için .db uzantılı olarak kayıt edip harici bir program ile " http://sqlitebrowser.org/ " içindeki kayıtlı datayı  görebiliyorum ios için bu tarz bir yol varmı onu merak etmiştim
tarafından
core data aslında bir database değildir. object graph modeline dayalı bir storage sistemidir. Bu nedenle tutulan verinin dışarı aktarılması ve harici bir tool'da görüntülenmesi gibi bir durum söz konusu değil maalesef.
tarafından
Tamam yasin hocam anladım teşekkür ederim.
tarafından
Hocam ufak bi sorum olacak onuda buradan sorayım mesela ben kullandığım global değişkenler için ayrı bir class oluşturup get set methodları kullanıp erişimi bu şekilde sağlamak istiyorum en azından android'te bu şekilde çalışıyorum bunu ios ta nasıl yapabilirim.
tarafından
core data verileriniz için mi ,yoksa genel olarak projeniz de global'leri nasıl kullanabileceğinizi mi soruyorsunuz?
tarafından
Genel olarak hocam bir değişkeni bir controllerde set ederken diğerinde  get etmek istiyorum
tarafından
mesela aşağıdaki gibi appDelegate içerisinde Global değişkenler tanımlayabilrsiniz.
aşağıda verdiğim örnek de bulunan myBoolean değişkenine diğer class'lar dan
Globals.variables.myBoolean ile erişim sağlayabilirsiniz.




import UIKit


class Variables:NSObject {
    var myBoolean:Bool = false {
        didSet {
            println("myBoolean is setted:\(self.myBoolean)")
        }
    }
}

struct Globals {
    static var variables:Variables! = nil
}

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        Globals.variables = Variables()
        return true
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
}
tarafından
Anladım hocam tekrardan teşekkür ediyorum.
...