The function below is used to get data for tableview
func fetchProfiles() -> Array<User> {
var users: Array<User> = []
let fetchRequest: NSFetchRequest<Profile> = Profile.fetchRequest()
let fetchedData = try! context.fetch(fetchRequest)
if (!fetchedData.isEmpty) {
print(fetchedData)
for i in 0...fetchedData.count {
var user: User = User()
user.userName = fetchedData[i].profileName
user.userSurname = fetchedData[i].profileSurname
user.userPhoto = fetchedData[i].profilePhoto
users.append(user)
}
return users
}
else {
return users
}
}
"User" is a simple struct. "Profile" is an entity in Core Data. I create an array of structs to use them for cells in table. Code has no errors(for xCode). When there is no fetched data, it skips appending array, but when there is some info, app crashes with error:
fatal error: NSArray element failed to match the Swift Array Element
type
For unknown reason, there were problems with auto generated headers of Core Data entities. I just deleted all files from folder:
/Users/user/Library/Developer/Xcode/DerivedData/MyApp/Build/Intermediates/MyApp.build/Debug-iphonesimulator/MyApp.build/DerivedSources/CoreDataGenerated
then cleaned my project with
command+shift+k
checked my core data model for some possible issues and rebuilt the whole project.
Magic.
Related
I am currently working on a project with a multi user system. The user is able to create new profiles which are saved persistently using CoreData.
My problem is: Only one profile can be the active one at a single time, so I would like to get the ObjectID of the created profile and save it to UserDefaults.
Further I was thinking that as soon as I need the data of the active profile, I can simply get the ObjectID from UserDefaults and execute a READ - Request which only gives me back the result with that specific ObjectID.
My code so far for SAVING THE DATA:
// 1. Create new profile entry to the context.
let newProfile = Profiles(context: context)
newProfile.idProfileImage = idProfileImage
newProfile.timeCreated = Date()
newProfile.gender = gender
newProfile.name = name
newProfile.age = age
newProfile.weight = weight
// 2. Save the Object ID to User Defaults for "activeUser".
// ???????????????????
// ???????????????????
// 3. Try to save the new profile by saving the context to the persistent container.
do {
try context.save()
} catch {
print("Error saving context \(error)")
}
My code so far for READING THE DATA
// 1. Creates an request that is just pulling all the data.
let request: NSFetchRequest<Profiles> = Profiles.fetchRequest()
// 2. Try to fetch the request, can throw an error.
do {
let result = try context.fetch(request)
} catch {
print("Error reading data \(error)")
}
As you can see, I haven't been able to implement Part 2 of the first code block. The new profile gets saved but the ObjectID isn't saved to UserDefaults.
Also Party 1 of the second code block is not the final goal. The request just gives you back all the data of that entity, not only the one with the ObjectID I stored in User Defaults.
I hope you guys have an idea on how to solve this problem.
Thanks for your help in advance guys!
Since NSManagedObjectID does not conform to one of the types handled by UserDefaults, you'll have to use another way to represent the object id. Luckily, NSManagedObjectID has a uriRepresentation() that returns a URL, which can be stored in UserDefaults.
Assuming you are using a NSPersistentContainer, here's an extension that will handle the storage and retrieval of a active user Profile:
extension NSPersistentContainer {
private var managedObjectIDKey: String {
return "ActiveUserObjectID"
}
var activeUser: Profile? {
get {
guard let url = UserDefaults.standard.url(forKey: managedObjectIDKey) else {
return nil
}
guard let managedObjectID = persistentStoreCoordinator.managedObjectID(forURIRepresentation: url) else {
return nil
}
return viewContext.object(with: managedObjectID) as? Profile
}
set {
guard let newValue = newValue else {
UserDefaults.standard.removeObject(forKey: managedObjectIDKey)
return
}
UserDefaults.standard.set(newValue.objectID.uriRepresentation(), forKey: managedObjectIDKey)
}
}
}
This uses a method on NSPersistentStoreCoordinator to construct a NSManagedObjectID from a URI representation.
I have users collection that has sub collection called attendedEvents like the picture below. as you can see there are 3 documents in the attendedEvents sub collection
I try to get all documents available on that sub collection by using the code below, I just want to get it all, without order, limit or anything using getDocuments
func getAttendedEventsFromBeginning(completion: #escaping (_ eventID: [String]?,QueryDocumentSnapshot?)->Void) {
FirestoreDocumentReference.users(uidUser: uid).reference().collection("attendedEvents")
.getDocuments { (snapshot, error) in
let lastDocument = snapshot?.documents.last
if let error = error {
completion(nil,lastDocument)
print("Error when fetching attended events documents in user subcollection: \(error.localizedDescription)")
} else {
print("Successfully fetching attended events documents in user subcollection from Firestore ")
guard let documentsSnapshot = snapshot else {
completion(nil,lastDocument)
return
}
let eventDocuments = documentsSnapshot.documents
print("xxxxx")
print(eventDocuments)
var attendedeventIDs = [String]()
for document in eventDocuments {
let eventDictionary = document.data()
let theEvent = eventDictionary["eventID"] as! String
attendedeventIDs.append(theEvent)
}
print(attendedeventIDs)
completion(attendedeventIDs,lastDocument)
}
}
}
but as a result, I just got 2 document snapshot, it should be 3 documents
but if I delete the app from simulator and install it again, I got all the three data. is is cached or what?
I have been in the same situation. In my case, this happens because in my document it only contains subcollection. And that will cause the document itself to not be shown in queries or snapshots.
My walkaround method is to add some random info in the document to make it exist.
How can I sync my server data (an array of dictionary) to my core data in scenarios below more efficiently?
update item in core data if item's attribute is changed in server
add item if not found in core data
finally, delete items in core data which not exist in server
My idea is as below but wonder if there is other more proper way or 3rd party library available to update my core data?
for coreItem in coreData {
coreItem.isSynced = false
for serverItem in serverData {
let predicate = NSPredicate(format: "%s == %s", coreItem.ID , serverItem.ID )
fetchRequest.predicate = predicate
var error: NSError?
let fetchedResults = managedObjectContext!.executeFetchRequest(fetchRequest, error: &error) as? [item]
if let results = fetchedResults {
coreItem.isSynced = true
if (results.count > 0) {
//TODO --> item exists in core data. proceed to update all attributes for this item.
}
else {
//TODO --> item does not exist in core data. proceed to add item to core data.
}
}
}
if (!coreItem.isSynced) {
//TODO --> item is not found in server. proceed to delete data.
}
}
I'm new to Realm and iOS. I'm working on an app(written in Swift) that has a feature for the user to search for golf courses. I have a JSON file with roughly 18K courses in it. So I wanted to use Realm so I can quickly search through these courses in my app without it slowing down the user experience. I was able to get my JSON file written to the Realm Browser and can retrieve and search through the items, which has made it a LOT faster.
The problem I'm is I have the code in my App Delegate because I wanted to send the JSON items to my Realm Browser upon the app's launch. But if the app is started again then it writes the JSON file again, which creates duplicate golf courses in my Realm Browser.
Any suggestions on how I should do this so I can write the JSON file to the browser without getting duplicates each time the app is launched?
Thanks!
My code for writing my JSON file to my Realm Browser:
let dataManager = DataManager.getGolfCoursesFromFileWithSuccess { (data) -> Void in
let json = JSON(data: data)
if let courseArray = json.array {
for course in courseArray {
let golfCourseName: String? = course["biz_name"].string
let city: String? = course["e_city"].string
let state: String? = course["e_state"].string
if golfCourseName != nil {
let course = Course()
course.name = golfCourseName!
course.city = city!
course.state = state!
let realm = try! Realm()
try! realm.write {
realm.add(course)
}
}
}
}
}
Just check to see if the data is stored already.
I.e.
if try! Realm().objects(GolfCourse).count == 0 {
// your loading code here.
}
I figured it out. Because this was a data set that I wanted the user to have when they initially start using the app I learned how to bundle a Realm file with this data and put the file directly in my Xcode project. I then configured this file using the method:
"NSBundle.mainBundle().pathForResource("MyBundledData", ofType:"realm")"
I'm saving objects to core data from a JSON, which I get using a for loop (let's say I called this setup function.
Because the user might stop this loop, the objects saved in core data will be partial. The user can restart this setup function, restarting the parsing and the procedure to save object to core data.
Now, I'm getting duplicated objects in core data if I restart the setup().
The object has an attribute which is id.
I've thought I could fetch first objects that could eventually already exist in core data, save them to an array (a custom type one), and test for each new object to add to core data if already exist one with the same id.
The code used is the following:
if !existingCards.isEmpty {
for existingCard in existingCards {
if id == existingCard.id {
moc.deleteObject(existingCard)
println("DELETED \(existingCard.name)")
}
}
}
...
// "existingCards is the array of object fetched previously.
// Code to save the object to core data.
Actually, the app return
EXC_BAD_ACCESS(code=1, address Ox0)
Is there an easier way to achieve my purpose or what should I fix to make my code work? I'm quite new to swift and I can't figure other solution.
The main purpose is to delete duplicated core data, BTW.
Swift 4 code to delete duplicate object:
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Card")
var resultsArr:[Card] = []
do {
resultsArr = try (mainManagedObjectContext!.fetch(fetchRequest) as! [Card])
} catch {
let fetchError = error as NSError
print(fetchError)
}
if resultsArr.count > 0 {
for x in resultsArr {
if x.id == id {
print("already exist")
mainManagedObjectContext.deleteObject(x)
}
}
}
At the end, I managed to make it work.
I had to rewrite my code, because I realized moc.deleteObject() works with a fetch before, which in my previous code wasn't in the same function, but it was in viewDidLoad().
// DO: - Fetch existing cards
var error: NSError?
var fetchRequest = NSFetchRequest(entityName: "Card")
if let results = moc.executeFetchRequest(fetchRequest, error: &error) as? [Card] {
if !results.isEmpty {
for x in results {
if x.id == id {
println("already exist")
moc.deleteObject(x)
}
}
}
} else {
println(error)
}
No more existingCards, the result of the the fetch is now processed as soon as possible. Something isn't clear to me yet, but now my code works. If you have any improvements/better ways, they're welcome.
P.S.: I actually found Apple reference useful but hard to understand because I don't know Obj-C. Often I can figure what the code do, but in swift functions and properties are a bit different.