2015-09-03 5 views
5

È possibile impostare un attributo su nil utilizzando NSBatchUpdateRequest? Passando NSNull()-propertiesToUpdate non funziona:Impostazione attributo dati principali su zero con NSBatchUpdateRequest

let unlockRequest = NSBatchUpdateRequest(entityName: "MyEntity") 
unlockRequest.predicate = NSPredicate(format: "self in %@", myObjectIDs) 
unlockRequest.propertiesToUpdate = ["lockDate": NSNull()] 
var error: NSError? 
myContext.executeRequest(unlockRequest, error: &error) 
if let error = error { 
    log.error("Failed to unlock: \(error)") 
} 

non ottengo errori, ma non cancella la data.

Ho anche provato a impostarlo su NSExpression(forConstantValue: NSNull()), ma non funziona (l'argomento forConstantValue non accetta un valore facoltativo, quindi non posso passarlo a nil.).

+0

Hai provato NSExpression (constantValue: nil)? – Willeke

+0

@Willeke Purtroppo non accetta un valore opzionale, quindi non posso passarlo nullo. – Sencha

+0

@Sencha hai trovato una soluzione per questo? – Serluca

risposta

0

Questo sembra funzionare correttamente in Objective-C:

NSBatchUpdateRequest *batch = [[NSBatchUpdateRequest alloc] initWithEntityName:entityName]; 
NSExpression *value = [NSExpression expressionForConstantValue: nil]; 
batch.propertiesToUpdate = @{@"lockDate": value}; 
batch.resultType = NSUpdatedObjectsCountResultType; 
NSError *error;     
NSBatchUpdateResult *results = [self.privateContext executeRequest:batch error:&error]; 
0

API corrente permette di passare nil come valore costante.

unlockRequest.propertiesToUpdate = ["lockDate": NSExpression(forConstantValue: nil)] 
+0

Vedo che la firma del metodo ora accetta gli optionals di Swift. Se questo è verificato per funzionare allora quello sarebbe il problema risolto! 'NSExpression.init (forConstantValue obj: Any?)' –

+0

Il compilatore non si lamenta, ma quando eseguo la richiesta genera un'eccezione: 'Terminazione dell'app a causa dell'eccezione non rilevata 'NSInvalidArgumentException', motivo: 'Chiave stringa non valida (null) passato a propertiesToUpdate: '' – blwinters

+0

L'intestazione per' NSBatchUpdateRequest.propertiesToUpdate' dice: "Le espressioni possono essere qualsiasi' NSExpression' che restituisce un valore scalare. " – blwinters

Problemi correlati