Remove Object from runTransactionBlock Array [Swift Firebase] - arrays

I'm trying to use Firebase and runTransactionBlock to update an Array on a server. I previously was able to add the uid of userToFollow to user's Following node using the solution found on Synchronized Array (for likes/followers) Best Practice [Firebase Swift]. Now I am trying to enable unfollowing by removing the uid of userToFollow from user's Following node, but despite the runTransactionBlock succeeding the data is not getting updated. Here is my code:
static func unfollow(user: FIRUser, userToUnfollow: FIRUser) {
self.database.child("users/"+(user.uid)+"/Following").runTransactionBlock({ (currentData: FIRMutableData!) -> FIRTransactionResult in
var value = currentData?.value as? Array<String>
if (value == nil) {
print("unfollow - user has never followed user ID " + userToUnfollow.uid)
FIRTransactionResult.abort()
} else {
value = value!.filter() {$0 != userToUnfollow.uid}
}
currentData.value = value!
return FIRTransactionResult.successWithValue(currentData)
}) { (error, committed, snapshot) in
if let error = error {
print("unfollow - update user unfollowing transaction, please try again - EXCEPTION: " + error.localizedDescription)
} else {
print("unfollow - update user unfollowing success")
}
}
}
I am trying to update the array by first sorting out the uid of userToUnfollow then syncing this data with Firebase. How can I get this to actually remove the node? Thanks.

Related

An unexpected null key in HashSet, the first be added not the rest

I'm learning Activiti 7, I drew a BPMN diagram as below:
When the highlight1 UserTask has been completed but the highlight2 UserTask is still pending, I ran the following code to highlight the completed flow element.
private AjaxResponse highlightHistoricProcess(#RequestParam("instanceId") String instanceId,
#AuthenticationPrincipal UserInfo userInfo) {
try {
// Get the instance from the history table
HistoricProcessInstance instance = historyService
.createHistoricProcessInstanceQuery().processInstanceId(instanceId).singleResult();
BpmnModel bpmnModel = repositoryService.getBpmnModel(instance.getProcessDefinitionId());
Process process = bpmnModel.getProcesses().get(0);
// Get all process elements, including sequences, events, activities, etc.
Collection<FlowElement> flowElements = process.getFlowElements();
Map<String, String> sequenceFlowMap = Maps.newHashMap();
flowElements.forEach(e -> {
if (e instanceof SequenceFlow) {
SequenceFlow sequenceFlow = (SequenceFlow) e;
String sourceRef = sequenceFlow.getSourceRef();
String targetRef = sequenceFlow.getTargetRef();
sequenceFlowMap.put(sourceRef + targetRef, sequenceFlow.getId());
}
});
// Get all historical Activities, i.e. those that have been executed and those that are currently being executed
List<HistoricActivityInstance> actList = historyService.createHistoricActivityInstanceQuery()
.processInstanceId(instanceId)
.list();
// Each history Activity is combined two by two
Set<String> actPairSet = new HashSet<>();
for (HistoricActivityInstance actA : actList) {
for (HistoricActivityInstance actB : actList) {
if (actA != actB) {
actPairSet.add(actA.getActivityId() + actB.getActivityId());
}
}
}
// Highlight Link ID
Set<String> highSequenceSet = Sets.newHashSet();
actPairSet.forEach(actPair -> {
logger.info("actPair:{}, seq:{}", actPair, sequenceFlowMap.get(actPair));
highSequenceSet.add(sequenceFlowMap.get(actPair));
logger.info("{}",highSequenceSet.toString());
});
// Get the completed Activity
List<HistoricActivityInstance> finishedActList = historyService
.createHistoricActivityInstanceQuery()
.processInstanceId(instanceId)
.finished()
.list();
// Highlight the completed Activity
Set<String> highActSet = Sets.newHashSet();
finishedActList.forEach(point -> highActSet.add(point.getActivityId()));
// Get the pending highlighted node, i.e. the currently executing node
List<HistoricActivityInstance> unfinishedActList = historyService
.createHistoricActivityInstanceQuery()
.processInstanceId(instanceId)
.unfinished()
.list();
Set<String> unfinishedPointSet = Sets.newHashSet();
unfinishedActList.forEach(point -> unfinishedPointSet.add(point.getActivityId()));
...
return AjaxResponse.ajax(ResponseCode.SUCCESS.getCode(),
ResponseCode.SUCCESS.getDesc(),
null);
} catch (Exception e) {
e.printStackTrace();
return AjaxResponse.ajax(ResponseCode.ERROR.getCode(),
"highlight failure",
e.toString());
}
}
Please see this piece of code:
// Highlight Link ID
Set<String> highSequenceSet = Sets.newHashSet();
actPairSet.forEach(actPair -> {
logger.info("actPair:{}, seq:{}", actPair, sequenceFlowMap.get(actPair));
highSequenceSet.add(sequenceFlowMap.get(actPair));
logger.info("{}",highSequenceSet.toString());
});
It was expected to get 2 elements in the highSequenceSet, but it got 3, with a unexpected null.
The log printed in the console was:
Why is the first null added to the HashSet but not the rest?
Why is the first null added to the HashSet but not the rest?
HashSet implements the Set interface, duplicate values are not allowed.

kotlin Firebase Variable path

Quick question.
While I write away my data to my firebase database I'd like to order it.
since i'm making an Album collection list I'd love to have it like the picture underneath.. but with bands and albums tho.
the only problem is that I can't read my write string.. in this case $Uname
I can only read the data correctly if I hardcode the location in my .getReference()
my data upload code:
//values
val number = Number_tv.text.toString()
val Uname = Username_TV.text.toString()
val ref = FirebaseDatabase.getInstance().getReference("/userdata/$Uname/$number _key")
val user = User(Username_TV.text.toString(), Email_TV.text.toString(), albumimageURL)
//code
if (number.isEmpty()){
Toast.makeText(this, "Number is empty", Toast.LENGTH_SHORT).show()
}
else{
ref.setValue(user)
.addOnSuccessListener {
Log.d("mainacti", "upload succesful")
}
.addOnFailureListener {
Log.d("mainacti", "I do not work")
}}
}
my data download code:
val ref = FirebaseDatabase.getInstance().getReference("/userdata/Heinz")
ref.addListenerForSingleValueEvent(object: ValueEventListener{
override fun onDataChange(p0: DataSnapshot) {
val adapter = GroupAdapter<GroupieViewHolder>()
p0.children.forEach {
Log.d("Albumlist", it.toString())
val user = it.getValue(User::class.java)
if (user != null){
adapter.add(UserItem(user))
}
}
recyclerview_album.adapter = adapter
}
override fun onCancelled(error: DatabaseError) {
}
})
}
Both codes are in different classes
here's a Log for the reference ("/userdata")
2021-04-16 13:00:42.080 6914-6914/com.example.firetrier D/Albumlist: DataSnapshot { key = Hans, value = {1 _key={albumimageURL=https://firebasestorage.googleapis.com/v0/b/fireupload-4be26.appspot.com/o/albumcover%2Fcb06a4c4-8795-4103-99ef-c2bd55498b2e?alt=media&token=7d3d305e-c266-41fb-b466-cab548fbdd35, email=hans#test.com, username=Hans}} }
2021-04-16 13:00:42.088 6914-6914/com.example.firetrier D/Albumlist: DataSnapshot { key = Heinz, value = {1 _key={albumimageURL=https://firebasestorage.googleapis.com/v0/b/fireupload-4be26.appspot.com/o/albumcover%2Ff00f6651-9ccf-4533-8e1c-af103a1a17e6?alt=media&token=b6e4ad2f-cb4f-47d5-8f00-02bdc4765c00, email=Heinz#doof.com, username=Heinz}, 5 _key={albumimageURL=https://firebasestorage.googleapis.com/v0/b/fireupload-4be26.appspot.com/o/albumcover%2Fc9bb80aa-15a2-48cb-b6f5-6b7a59b11d8d?alt=media&token=facd516d-c3e3-4b61-aa09-23ea9062ccd3, email=hans#test.com, username=Heinz}} }
is there a way to use the value instead of the key?
if I adapt the reference to one of the usernames this is the log
2021-04-16 13:05:54.028 7629-7629/com.example.firetrier D/Albumlist: DataSnapshot { key = 1 _key, value = {albumimageURL=https://firebasestorage.googleapis.com/v0/b/fireupload-4be26.appspot.com/o/albumcover%2Ff00f6651-9ccf-4533-8e1c-af103a1a17e6?alt=media&token=b6e4ad2f-cb4f-47d5-8f00-02bdc4765c00, email=Heinz#doof.com, username=Heinz} }
2021-04-16 13:05:54.040 7629-7629/com.example.firetrier D/Albumlist: DataSnapshot { key = 5 _key, value = {albumimageURL=https://firebasestorage.googleapis.com/v0/b/fireupload-4be26.appspot.com/o/albumcover%2Fc9bb80aa-15a2-48cb-b6f5-6b7a59b11d8d?alt=media&token=facd516d-c3e3-4b61-aa09-23ea9062ccd3, email=hans#test.com, username=Heinz} }

How can I get an item by a string key using web api?

I can't figure out how to hit an endpoint like "api/GetItems/AB123" (AB123 of course being a string) and have it return that item from my data set. I read the docs on the FindAsync() method and it seemed to indicate that it would accept a string by default. Is there something I need to do to 'id' before passing it into FindAsync()? My DB does not have a primary key, if that matters. (I can't change that either, this is legacy data and I have no control over schema)
My db doesn't have a PK ID field. I need to do the next best thing and target a unique string field.
My GET method:
// GET: api/Items/5
[HttpGet("{id}")]
public async Task<ActionResult<Item>> GetItem(string id)
{
var item = await _context.Items.FindAsync(id); // Error happens here: "InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Int64'."
if (item == null)
{
return NotFound();
}
return item;
}
Relevant from my model:
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string ItemId { get; set; }
Hello and welcome to the Stack Overflow Community!!
Instead of find you could do .SingleAsync() like the below.
You could also do a .Where(x => x.ItemId == id).SingleAsync(). But this is up to you.
// GET: api/Items/5
[HttpGet("{id}")]
public async Task<ActionResult<Item>> GetItem(string id)
{
var item = await _context.Items.SingleAsync(x => x.ItemId == id);
if (item == null)
{
return NotFound();
}
return item;
}
From your error it is obvious that int is expected in FindById method. Can you check the field type in database but from your model I would say that you don't have correct type.
String can't be used as Identity in this way because SQL Server doesn't know how to generate value for that.
You can check this post for more details on that: DatabaseGeneratedOption.Identity not generating an Id
So to conclude, you should check what do you really have in the database to determine is your model wrong.
If that is not the case and you do have a string in the db you should just retrieve item by using SingleAsync method (note that it will throw exception if the id is wrong).
var item = await _context.Items.SingleAsync(e => e.ItemId == id);
If you don't want an exception if the id doesn't exist you can use:
var item = await _context.Items.SingleOrDefaultAsync(e => e.ItemId == id);
which will return null for non existent id.

Manage http error codes with Codename One

I wrote:
private RequestBuilder getPostRequest(String api) {
return Rest.post(url + api)
.jsonContent()
.header("wsc-access-key", WowzaAccount.getAccessKey())
.header("wsc-api-key", WowzaAccount.getRestKey());
}
getPostRequest("live_streams").body(json).fetchAsJsonMap(new OnComplete<Response<Map>>() {
#Override
public void completed(Response<Map> v) {
if (v.getResponseCode() == 201) {
// success
Map<String, Object> response = v.getResponseData();
name = (String) response.get("name");
id = (String) response.get("id");
connection_code = (String) response.get("connection_code");
Log.p("WowzaLiveStream -> (Code 201) Successfully created live stream with name " + name, Log.DEBUG);
onComplete.completed(instance);
} else if (v.getResponseCode() == 401) {
Log.p("WowzaLiveStream -> (Code 401) Unauthorized, failed to create live stream with name " + params.name.get(), Log.DEBUG);
onFail.run();
} else if (v.getResponseCode() == 422) {
Log.p("WowzaLiveStream -> (Code 422) Unprocessable Entity, failed to create live stream with name " + params.name.get(), Log.DEBUG);
onFail.run();
} else {
Log.p("WowzaLiveStream -> Unknow response with code " + v.getResponseCode() + ", failed to create live stream with name " + params.name.get(), Log.DEBUG);
onFail.run();
}
}
});
The problem is that when I get a 422 response code my onFail callback is not called. Instead a Dialog appears. I suppose that this dialog is invoked by the default addNetworkErrorListener code in the init(). However... I cannot (and I don't want to) disable the default addNetworkErrorListener code, because I'm writing a new CN1Lib. Instead I need that in this case, and only in this case, the network error listener should not be invoked and instead the failure callback that I wrote should be run.
It's more appropriate, in this case, to call the network error listener only if the Internet connection is lost.
Thank you
You need to explicitly catch the error code callback as the callback might have a different format than the main JSON:
private RequestBuilder getPostRequest(String api) {
return Rest.post(url + api)
.jsonContent()
.header("wsc-access-key", WowzaAccount.getAccessKey())
.header("wsc-api-key", WowzaAccount.getRestKey())
.onErrorCodeJSON(map -> {
// process error response
});
}

Set field Accessibility to Custom Salesforce Lead field from Java code

I am working around with Salesforce and force.com API and metadata API, version 36.
I can create a custom field in a Lead object but by default I can see it's hidden and this means I cannot create a new Lead with these custom fields because it returns a bad request (400 status code).
Is there any way by Code to set the custom field Visible?
public boolean createCustomExtTextField(String name, LoginResult metadataLoginResult, int length) {
boolean success = false;
CustomField cs = new CustomField();
cs.setFullName("Lead."+name+"__c");
cs.setLabel("Custom"+name+"Field");
cs.setType(FieldType.LongTextArea);
cs.setLength(length);
cs.setVisibleLines(50); // max 50
try {
MetadataConnection metadataConnection = createMetadataConnection(metadataLoginResult);
SaveResult[] results = metadataConnection.createMetadata(new Metadata[] { cs });
for (SaveResult r : results) {
if (r.isSuccess()) {
success = true;
} else {
System.out.println("Errors were encountered while creating " + r.getFullName());
for (com.sforce.soap.metadata.Error e : r.getErrors()) {
System.out.println("Error message: " + e.getMessage());
System.out.println("Status code: " + e.getStatusCode());
}
}
}
} catch (ConnectionException e) {
e.printStackTrace();
}
return success;
}
I am googling a lot and don't find something that actually helped. So, any hints are welcomed. Thank you.
Finally found a solution to this. I final one for me was to make all custom fields REQUIRED.
CustomField cs = new CustomField();
cs.setFullName("Lead.YourCompanyName" + name + "__c");
cs.setLabel("YourCompanyName" + name);
cs.setRequired(true);
...
com.sforce.soap.enterprise.LoginResult metadataLoginResult = operations.loginToMetadata(username, password, "https://login.salesforce.com/services/Soap/c/36.0");
...
private boolean createFieldInMetadata(LoginResult metadataLoginResult, CustomField cs) {
boolean success = false;
try {
MetadataConnection metadataConnection = createMetadataConnection(metadataLoginResult);
SaveResult[] results = metadataConnection.createMetadata(new Metadata[] { cs });
for (SaveResult r : results) {
if (r.isSuccess()) {
success = true;
} else {
System.out.println("Errors were encountered while creating " + r.getFullName());
for (com.sforce.soap.metadata.Error e : r.getErrors()) {
System.out.println("Error message: " + e.getMessage());
System.out.println("Status code: " + e.getStatusCode());
}
}
}
} catch (Exception e) {
}
return success;
}
And so it will appear in the page layout. Very important to know, a required field cannot have just an empty value set, it must be something. So if not all custom fields are required in your logic and you wanna avoid the entire process of unzipping page layout and zipping it back (however it may be done) just add "N/A" or any char at choice to the required by code but not your project custom fields.
I managed to make the custom Field Level Security visible for "Admin" profile but not Field Accessability to visible. The latter is untested.

Resources