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

Post json array to web service not working
Why does LambdaExpression.Compile() work on iOS (Xamarin)?
Build failed--attribute must have a defined type
Firebase iOS query not ordering as expected
Swift code compiles fine on iOS, gives warning on OSX: Enum case pattern cannot match values of the non-enum type 'SKPaymentTransactionState`
Data does not get updated on the table view from one view to the next
In swift, when one property has no default value, the default values held by other properties become useless?
How can I loop through the coordinates of a CGPath?
AVAsset not recognising NSURL - Crashing - iOS
Size class specifically for portrait 3.5 inch (iPhone 4S) XCode 6?
Two problems with UISearchBar when it is embedded in UINavigationBar
prepareForSegue not opening new view
How to code for a password needing a number and capital letter in objective c
How to disable extension sharing via excludedActivityTypes?
Custom webview keyboard issues
Set Cell Background Color from index path corresponding Color in Parse

Categories

HOME
redirect
dynamics-crm-365
elf
servlets
youtube
apacheds
bolt-cms
anylogic
garbage-collection
feed
xcode8
spi
oracle-adf
mobilefirst-adapters
cobol
uwsgi
frequency
partitioning
qualtrics
libssh
afnetworking-2
plc
gitlab-ci-runner
lexikjwtauthbundle
stm32f4discovery
dotnetbar
cayley
similarity
line-breaks
shape
precision
code-signing
desire2learn
google-geocoder
asp.net-identity-2
silverlight-5.0
instance
localdb
sqlite.swift
machine-language
spring-websocket
lifecycle
powerbuilder-conversion
packaging
jsonstore
worldpay
php-deployer
ssms-2012
libev
ietf-netconf
playback
jquery-ui-accordion
tastypie
qtcpserver
kombu
configuration-profile
tfs2008
logstash-jdbc
statistics-bootstrap
directsound
ellipsis
google-api-ruby-client
lidar
balana
skrollr
java-gstreamer
wgs84
uac
session-replication
hypervisor
wss4j
solr-boost
fastparse
conditional-statements
words
mpj-express
syndication-feed
globus-toolkit
image-scaling
tinkerpop-frames
twill
fig
nine-patch
deap
bochs
micro-orm
xcode4.2
double-precision
icon-fonts
cassette
browserid
ms-access-97
ed
intel-8080
openid4java
closest
mkreversegeocoder
hunchentoot
cracker

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