ios


Deleting Data instead of using Migration


Deleting Data instead of using Migration
In my code I added a property to my realm class. Is there a way to delete the data and get rid off my whole database instead of using migration?
I'm still testing that's why I probably won't need Migration for now.
Please help me, I'm really struggling with using Migration
Thank you
if you are only testing locally you can delete the app and re-install. If you already have your app on the App Store and want to prepare migrations for users you can have a look at the Migrations Sample App from Realm.
Try this on your AppDelegate:
import UIKit
import RealmSwift
// Old data models
/* V0
class Person: Object {
dynamic var firstName = ""
dynamic var lastName = ""
dynamic var age = 0
}
*/
// V1
class Person: Object {
dynamic var fullName = "" // combine firstName and lastName into single field
dynamic var age = 0
}
class Person: Object {
dynamic var fullName = ""
dynamic var age = 0
}
func bundleURL(_ name: String) -> URL? {
return Bundle.main.url(forResource: name, withExtension: "realm")
}
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = UIViewController()
window?.makeKeyAndVisible()
// copy over old data files for migration
let defaultURL = Realm.Configuration.defaultConfiguration.fileURL!
let defaultParentURL = defaultURL.deletingLastPathComponent()
if let v0URL = bundleURL("default-v0") {
do {
try FileManager.default.removeItem(at: defaultURL)
try FileManager.default.copyItem(at: v0URL, to: defaultURL)
} catch {}
}
// define a migration block
// you can define this inline, but we will reuse this to migrate realm files from multiple versions
// to the most current version of our data model
let migrationBlock: MigrationBlock = { migration, oldSchemaVersion in
if oldSchemaVersion < 1 {
migration.enumerateObjects(ofType: Person.className()) { oldObject, newObject in
if oldSchemaVersion < 1 {
// combine name fields into a single field
let firstName = oldObject!["firstName"] as! String
let lastName = oldObject!["lastName"] as! String
newObject?["fullName"] = "\(firstName) \(lastName)"
}
}
}
print("Migration complete.")
}
Realm.Configuration.defaultConfiguration = Realm.Configuration(schemaVersion: 2, migrationBlock: migrationBlock)
// print out all migrated objects in the default realm
// migration is performed implicitly on Realm access
print("Migrated objects in the default Realm: \(try! Realm().objects(Person.self))")
return true
}
}

Related Links

Subtracting numbers as Objects - Objective C
Xcode UITest - Unable to find buttons in a Cell containing Key Value Pairs
Submit to Google Form using objective c
How to dismiss modal ViewController from right to left?
Choose specific url
mapViewWillStartLoadingMap method does not called second time Swift
iPhone 6 splash screen resized after few second
How to return array of different enums based on a condition in Swift?
download image from web with progress to photos app AFNetworking 3
Swift convertPointFromView returns NaN when move back from child view
obejective c - Portential leak of an object stored into
When submitting a swift based cocoapod, I get this error when checking if the device is a simulator: code after 'return' will never be executed
“No such module 'Alamofire'” Xcode won't recognize Alamofire framework
Using dismissViewController on queue of multiple UIViewControllers with delegate
Running Parse Server with MongoDB on Digital Ocean
Fatal Error when connectting Objective-C framework to Swift iOS project (SpeechSDK framework)

Categories

HOME
ssis
whatsapp
coffeescript
serial-port
runnable
order
sql-server-2012
word-vba
api.ai
tsql
r-caret
base
zipfile
drag
gcov
research
overloading
threadpool
monaco-editor
factory-girl
image-gallery
thrift
revitpythonshell
azure-ad-graph-api
graphql-dotnet
laravel-valet
udid
magento2.0.2
react-redux-form
sqlite.swift
stackexchange.exceptional
nav
chamilo-lms
mask
ubuntu-server
bezier
angular-xeditable
partition
flags
dxgi
edeliver
website-hosting
wrk
fasta
android-touch-event
getelementbyid
waf
dr-memory
error-correction
ispconfig
evaluate
ubuntu-15.04
amazon-dynamodb-streams
heritrix
uploadify
ota
schematiq
occam-pi
coldfusion-10
ppl
cvc4
spatial-index
boost-coroutine
zim-database
mysql5
physx
jet.com-apis
cpp-netlib
spread-toolkit
master-theorem
xacml3
sirtrevor
b2b
magic-unipaas
azimuth
data-dictionary
batman.js
disabled-input
django-endless-pagination
onejar
couchrest
re2c
hresult
chromeless
hogan.js
word-2010
layout-manager
nsinvocation
azure-role-environment
jammit
eye-detection
nstreecontroller
maemo
corporate-policy

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App