2016-12-24 114 views
0

我在local2上使用local2,它在铬和iphone上本地工作,但似乎无法在Android设备上工作。Ionic 2 @ ionic/storage在Android设备上无法正常工作

我真的不想重写和使用Sqlite,因为我的数据比表结构更适合json结构。任何线索?

CODE

import { Injectable } from '@angular/core'; 
import { Storage } from '@ionic/storage'; 
import { Observable } from 'rxjs/Rx'; 

import { Customer } from '../pages/customer/customer.model' 

@Injectable() 
export class StorageService { 
    constructor(private _storage: Storage) { } 

    setSelectedCustomer(customer: Customer) { 
     return Observable.fromPromise(this._storage.set('selectedCustomer', customer)); 
    } 
} 

版本:

"@ionic/storage": "^1.1.7", 
"ionic-angular": "2.0.0-rc.3", 
"ionic-native": "2.2.3", 

感谢

+0

你能否提供一些代码片段并解释你已经尝试过什么? –

+0

总是有一些没有答案的问题,只知道如何点击投票。 – Milad

+0

谢谢@Milad我在想,但没有意思。 –

回答

2

它不是用一个好主意localStorage保存importa nt数据在混合应用程序中。有关Android上的问题以及iOS上的某些问题的报告太多。一般来说,这是由不正确的数据类型处理引起的。举例来说,你试图保存一些对象,但在Chrome中,Android可能会以不同的方式进行序列化。因此,最好是做手工,如:

this._storage.set('selectedCustomer', JSON.stringify(customer)) 

,然后使用JSON.parsethis article提到的仍然是问题从JSON字符串解析的对象。因此,最好是使用一些更强大的存储用户数据(如SQLite的)

另外,如果你不想改变从JSON到关系表结构,你可以尝试LokiJS

+0

对于其他NoSQL替代品,您可以查看类似Couchbase或PouchDB的内容。这里有一篇关于Couchbase + Ionic 2的文章:https://blog.couchbase.com/2016/december/data-synchronization-with-couchbase-in-ionic-2-hybrid-mobile-apps – Hod

0

我这样使用它和它的做工精细

this._storage.set('selectedCustomer', JSON.stringify(customer)); 

this._storage.get('selectedCustomer').then((selectedCustomer) => { 
    let json_selectedCustomer = JSON.parse(selectedCustomer); 
    }); 
+0

您正在使用承诺,并将转换为来自Promise的Observable。我不认为这是问题。 –

+0

我想提一下,当您使用存储作为键/值时,您必须将该值作为字符串发送。 –

相关问题